xref: /openbmc/linux/drivers/gpu/drm/qxl/qxl_kms.c (revision 95777591)
1 /*
2  * Copyright 2013 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Dave Airlie
23  *          Alon Levy
24  */
25 
26 #include "qxl_drv.h"
27 #include "qxl_object.h"
28 
29 #include <drm/drm_crtc_helper.h>
30 #include <linux/io-mapping.h>
31 
32 int qxl_log_level;
33 
34 static bool qxl_check_device(struct qxl_device *qdev)
35 {
36 	struct qxl_rom *rom = qdev->rom;
37 
38 	if (rom->magic != 0x4f525851) {
39 		DRM_ERROR("bad rom signature %x\n", rom->magic);
40 		return false;
41 	}
42 
43 	DRM_INFO("Device Version %d.%d\n", rom->id, rom->update_id);
44 	DRM_INFO("Compression level %d log level %d\n", rom->compression_level,
45 		 rom->log_level);
46 	DRM_INFO("%d io pages at offset 0x%x\n",
47 		 rom->num_io_pages, rom->pages_offset);
48 	DRM_INFO("%d byte draw area at offset 0x%x\n",
49 		 rom->surface0_area_size, rom->draw_area_offset);
50 
51 	qdev->vram_size = rom->surface0_area_size;
52 	DRM_INFO("RAM header offset: 0x%x\n", rom->ram_header_offset);
53 	return true;
54 }
55 
56 static void setup_hw_slot(struct qxl_device *qdev, int slot_index,
57 			  struct qxl_memslot *slot)
58 {
59 	qdev->ram_header->mem_slot.mem_start = slot->start_phys_addr;
60 	qdev->ram_header->mem_slot.mem_end = slot->end_phys_addr;
61 	qxl_io_memslot_add(qdev, slot_index);
62 }
63 
64 static uint8_t setup_slot(struct qxl_device *qdev, uint8_t slot_index_offset,
65 	unsigned long start_phys_addr, unsigned long end_phys_addr)
66 {
67 	uint64_t high_bits;
68 	struct qxl_memslot *slot;
69 	uint8_t slot_index;
70 
71 	slot_index = qdev->rom->slots_start + slot_index_offset;
72 	slot = &qdev->mem_slots[slot_index];
73 	slot->start_phys_addr = start_phys_addr;
74 	slot->end_phys_addr = end_phys_addr;
75 
76 	setup_hw_slot(qdev, slot_index, slot);
77 
78 	slot->generation = qdev->rom->slot_generation;
79 	high_bits = slot_index << qdev->slot_gen_bits;
80 	high_bits |= slot->generation;
81 	high_bits <<= (64 - (qdev->slot_gen_bits + qdev->slot_id_bits));
82 	slot->high_bits = high_bits;
83 	return slot_index;
84 }
85 
86 void qxl_reinit_memslots(struct qxl_device *qdev)
87 {
88 	setup_hw_slot(qdev, qdev->main_mem_slot, &qdev->mem_slots[qdev->main_mem_slot]);
89 	setup_hw_slot(qdev, qdev->surfaces_mem_slot, &qdev->mem_slots[qdev->surfaces_mem_slot]);
90 }
91 
92 static void qxl_gc_work(struct work_struct *work)
93 {
94 	struct qxl_device *qdev = container_of(work, struct qxl_device, gc_work);
95 
96 	qxl_garbage_collect(qdev);
97 }
98 
99 int qxl_device_init(struct qxl_device *qdev,
100 		    struct drm_driver *drv,
101 		    struct pci_dev *pdev)
102 {
103 	int r, sb;
104 
105 	r = drm_dev_init(&qdev->ddev, drv, &pdev->dev);
106 	if (r) {
107 		pr_err("Unable to init drm dev");
108 		goto error;
109 	}
110 
111 	qdev->ddev.pdev = pdev;
112 	pci_set_drvdata(pdev, &qdev->ddev);
113 	qdev->ddev.dev_private = qdev;
114 
115 	mutex_init(&qdev->gem.mutex);
116 	mutex_init(&qdev->update_area_mutex);
117 	mutex_init(&qdev->release_mutex);
118 	mutex_init(&qdev->surf_evict_mutex);
119 	qxl_gem_init(qdev);
120 
121 	qdev->rom_base = pci_resource_start(pdev, 2);
122 	qdev->rom_size = pci_resource_len(pdev, 2);
123 	qdev->vram_base = pci_resource_start(pdev, 0);
124 	qdev->io_base = pci_resource_start(pdev, 3);
125 
126 	qdev->vram_mapping = io_mapping_create_wc(qdev->vram_base, pci_resource_len(pdev, 0));
127 	if (!qdev->vram_mapping) {
128 		pr_err("Unable to create vram_mapping");
129 		r = -ENOMEM;
130 		goto error;
131 	}
132 
133 	if (pci_resource_len(pdev, 4) > 0) {
134 		/* 64bit surface bar present */
135 		sb = 4;
136 		qdev->surfaceram_base = pci_resource_start(pdev, sb);
137 		qdev->surfaceram_size = pci_resource_len(pdev, sb);
138 		qdev->surface_mapping =
139 			io_mapping_create_wc(qdev->surfaceram_base,
140 					     qdev->surfaceram_size);
141 	}
142 	if (qdev->surface_mapping == NULL) {
143 		/* 64bit surface bar not present (or mapping failed) */
144 		sb = 1;
145 		qdev->surfaceram_base = pci_resource_start(pdev, sb);
146 		qdev->surfaceram_size = pci_resource_len(pdev, sb);
147 		qdev->surface_mapping =
148 			io_mapping_create_wc(qdev->surfaceram_base,
149 					     qdev->surfaceram_size);
150 		if (!qdev->surface_mapping) {
151 			pr_err("Unable to create surface_mapping");
152 			r = -ENOMEM;
153 			goto vram_mapping_free;
154 		}
155 	}
156 
157 	DRM_DEBUG_KMS("qxl: vram %llx-%llx(%dM %dk), surface %llx-%llx(%dM %dk, %s)\n",
158 		 (unsigned long long)qdev->vram_base,
159 		 (unsigned long long)pci_resource_end(pdev, 0),
160 		 (int)pci_resource_len(pdev, 0) / 1024 / 1024,
161 		 (int)pci_resource_len(pdev, 0) / 1024,
162 		 (unsigned long long)qdev->surfaceram_base,
163 		 (unsigned long long)pci_resource_end(pdev, sb),
164 		 (int)qdev->surfaceram_size / 1024 / 1024,
165 		 (int)qdev->surfaceram_size / 1024,
166 		 (sb == 4) ? "64bit" : "32bit");
167 
168 	qdev->rom = ioremap(qdev->rom_base, qdev->rom_size);
169 	if (!qdev->rom) {
170 		pr_err("Unable to ioremap ROM\n");
171 		r = -ENOMEM;
172 		goto surface_mapping_free;
173 	}
174 
175 	if (!qxl_check_device(qdev)) {
176 		r = -ENODEV;
177 		goto surface_mapping_free;
178 	}
179 
180 	r = qxl_bo_init(qdev);
181 	if (r) {
182 		DRM_ERROR("bo init failed %d\n", r);
183 		goto rom_unmap;
184 	}
185 
186 	qdev->ram_header = ioremap(qdev->vram_base +
187 				   qdev->rom->ram_header_offset,
188 				   sizeof(*qdev->ram_header));
189 	if (!qdev->ram_header) {
190 		DRM_ERROR("Unable to ioremap RAM header\n");
191 		r = -ENOMEM;
192 		goto bo_fini;
193 	}
194 
195 	qdev->command_ring = qxl_ring_create(&(qdev->ram_header->cmd_ring_hdr),
196 					     sizeof(struct qxl_command),
197 					     QXL_COMMAND_RING_SIZE,
198 					     qdev->io_base + QXL_IO_NOTIFY_CMD,
199 					     false,
200 					     &qdev->display_event);
201 	if (!qdev->command_ring) {
202 		DRM_ERROR("Unable to create command ring\n");
203 		r = -ENOMEM;
204 		goto ram_header_unmap;
205 	}
206 
207 	qdev->cursor_ring = qxl_ring_create(
208 				&(qdev->ram_header->cursor_ring_hdr),
209 				sizeof(struct qxl_command),
210 				QXL_CURSOR_RING_SIZE,
211 				qdev->io_base + QXL_IO_NOTIFY_CMD,
212 				false,
213 				&qdev->cursor_event);
214 
215 	if (!qdev->cursor_ring) {
216 		DRM_ERROR("Unable to create cursor ring\n");
217 		r = -ENOMEM;
218 		goto command_ring_free;
219 	}
220 
221 	qdev->release_ring = qxl_ring_create(
222 				&(qdev->ram_header->release_ring_hdr),
223 				sizeof(uint64_t),
224 				QXL_RELEASE_RING_SIZE, 0, true,
225 				NULL);
226 
227 	if (!qdev->release_ring) {
228 		DRM_ERROR("Unable to create release ring\n");
229 		r = -ENOMEM;
230 		goto cursor_ring_free;
231 	}
232 	/* TODO - slot initialization should happen on reset. where is our
233 	 * reset handler? */
234 	qdev->n_mem_slots = qdev->rom->slots_end;
235 	qdev->slot_gen_bits = qdev->rom->slot_gen_bits;
236 	qdev->slot_id_bits = qdev->rom->slot_id_bits;
237 	qdev->va_slot_mask =
238 		(~(uint64_t)0) >> (qdev->slot_id_bits + qdev->slot_gen_bits);
239 
240 	qdev->mem_slots =
241 		kmalloc_array(qdev->n_mem_slots, sizeof(struct qxl_memslot),
242 			      GFP_KERNEL);
243 
244 	if (!qdev->mem_slots) {
245 		DRM_ERROR("Unable to alloc mem slots\n");
246 		r = -ENOMEM;
247 		goto release_ring_free;
248 	}
249 
250 	idr_init(&qdev->release_idr);
251 	spin_lock_init(&qdev->release_idr_lock);
252 	spin_lock_init(&qdev->release_lock);
253 
254 	idr_init(&qdev->surf_id_idr);
255 	spin_lock_init(&qdev->surf_id_idr_lock);
256 
257 	mutex_init(&qdev->async_io_mutex);
258 
259 	/* reset the device into a known state - no memslots, no primary
260 	 * created, no surfaces. */
261 	qxl_io_reset(qdev);
262 
263 	/* must initialize irq before first async io - slot creation */
264 	r = qxl_irq_init(qdev);
265 	if (r) {
266 		DRM_ERROR("Unable to init qxl irq\n");
267 		goto mem_slots_free;
268 	}
269 
270 	/*
271 	 * Note that virtual is surface0. We rely on the single ioremap done
272 	 * before.
273 	 */
274 	qdev->main_mem_slot = setup_slot(qdev, 0,
275 		(unsigned long)qdev->vram_base,
276 		(unsigned long)qdev->vram_base + qdev->rom->ram_header_offset);
277 	qdev->surfaces_mem_slot = setup_slot(qdev, 1,
278 		(unsigned long)qdev->surfaceram_base,
279 		(unsigned long)qdev->surfaceram_base + qdev->surfaceram_size);
280 	DRM_INFO("main mem slot %d [%lx,%x]\n",
281 		 qdev->main_mem_slot,
282 		 (unsigned long)qdev->vram_base, qdev->rom->ram_header_offset);
283 	DRM_INFO("surface mem slot %d [%lx,%lx]\n",
284 		 qdev->surfaces_mem_slot,
285 		 (unsigned long)qdev->surfaceram_base,
286 		 (unsigned long)qdev->surfaceram_size);
287 
288 	INIT_WORK(&qdev->gc_work, qxl_gc_work);
289 
290 	return 0;
291 
292 mem_slots_free:
293 	kfree(qdev->mem_slots);
294 release_ring_free:
295 	qxl_ring_free(qdev->release_ring);
296 cursor_ring_free:
297 	qxl_ring_free(qdev->cursor_ring);
298 command_ring_free:
299 	qxl_ring_free(qdev->command_ring);
300 ram_header_unmap:
301 	iounmap(qdev->ram_header);
302 bo_fini:
303 	qxl_bo_fini(qdev);
304 rom_unmap:
305 	iounmap(qdev->rom);
306 surface_mapping_free:
307 	io_mapping_free(qdev->surface_mapping);
308 vram_mapping_free:
309 	io_mapping_free(qdev->vram_mapping);
310 error:
311 	return r;
312 }
313 
314 void qxl_device_fini(struct qxl_device *qdev)
315 {
316 	qxl_bo_unref(&qdev->current_release_bo[0]);
317 	qxl_bo_unref(&qdev->current_release_bo[1]);
318 	flush_work(&qdev->gc_work);
319 	qxl_ring_free(qdev->command_ring);
320 	qxl_ring_free(qdev->cursor_ring);
321 	qxl_ring_free(qdev->release_ring);
322 	qxl_gem_fini(qdev);
323 	qxl_bo_fini(qdev);
324 	io_mapping_free(qdev->surface_mapping);
325 	io_mapping_free(qdev->vram_mapping);
326 	iounmap(qdev->ram_header);
327 	iounmap(qdev->rom);
328 	qdev->rom = NULL;
329 }
330