blob: 2c6f9219e9d9961a411c85c1d42515a01a8d69c6 [file] [log] [blame]
Dave Airlief64122c2013-02-25 14:47:55 +10001/*
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 <linux/io-mapping.h>
30
31int qxl_log_level;
32
33static void qxl_dump_mode(struct qxl_device *qdev, void *p)
34{
35 struct qxl_mode *m = p;
36 DRM_DEBUG_KMS("%d: %dx%d %d bits, stride %d, %dmm x %dmm, orientation %d\n",
37 m->id, m->x_res, m->y_res, m->bits, m->stride, m->x_mili,
38 m->y_mili, m->orientation);
39}
40
41static bool qxl_check_device(struct qxl_device *qdev)
42{
43 struct qxl_rom *rom = qdev->rom;
44 int mode_offset;
45 int i;
46
47 if (rom->magic != 0x4f525851) {
48 DRM_ERROR("bad rom signature %x\n", rom->magic);
49 return false;
50 }
51
52 DRM_INFO("Device Version %d.%d\n", rom->id, rom->update_id);
53 DRM_INFO("Compression level %d log level %d\n", rom->compression_level,
54 rom->log_level);
55 DRM_INFO("Currently using mode #%d, list at 0x%x\n",
56 rom->mode, rom->modes_offset);
57 DRM_INFO("%d io pages at offset 0x%x\n",
58 rom->num_io_pages, rom->pages_offset);
59 DRM_INFO("%d byte draw area at offset 0x%x\n",
60 rom->surface0_area_size, rom->draw_area_offset);
61
62 qdev->vram_size = rom->surface0_area_size;
63 DRM_INFO("RAM header offset: 0x%x\n", rom->ram_header_offset);
64
65 mode_offset = rom->modes_offset / 4;
66 qdev->mode_info.num_modes = ((u32 *)rom)[mode_offset];
67 DRM_INFO("rom modes offset 0x%x for %d modes\n", rom->modes_offset,
68 qdev->mode_info.num_modes);
69 qdev->mode_info.modes = (void *)((uint32_t *)rom + mode_offset + 1);
70 for (i = 0; i < qdev->mode_info.num_modes; i++)
71 qxl_dump_mode(qdev, qdev->mode_info.modes + i);
72 return true;
73}
74
Dave Airliec9fdda22013-07-04 14:57:58 +100075static void setup_hw_slot(struct qxl_device *qdev, int slot_index,
76 struct qxl_memslot *slot)
77{
78 qdev->ram_header->mem_slot.mem_start = slot->start_phys_addr;
79 qdev->ram_header->mem_slot.mem_end = slot->end_phys_addr;
80 qxl_io_memslot_add(qdev, slot_index);
81}
82
Dave Airlief64122c2013-02-25 14:47:55 +100083static uint8_t setup_slot(struct qxl_device *qdev, uint8_t slot_index_offset,
84 unsigned long start_phys_addr, unsigned long end_phys_addr)
85{
86 uint64_t high_bits;
87 struct qxl_memslot *slot;
88 uint8_t slot_index;
Dave Airlief64122c2013-02-25 14:47:55 +100089
90 slot_index = qdev->rom->slots_start + slot_index_offset;
91 slot = &qdev->mem_slots[slot_index];
92 slot->start_phys_addr = start_phys_addr;
93 slot->end_phys_addr = end_phys_addr;
Dave Airliec9fdda22013-07-04 14:57:58 +100094
95 setup_hw_slot(qdev, slot_index, slot);
96
Dave Airlief64122c2013-02-25 14:47:55 +100097 slot->generation = qdev->rom->slot_generation;
98 high_bits = slot_index << qdev->slot_gen_bits;
99 high_bits |= slot->generation;
100 high_bits <<= (64 - (qdev->slot_gen_bits + qdev->slot_id_bits));
101 slot->high_bits = high_bits;
102 return slot_index;
103}
104
Dave Airliec9fdda22013-07-04 14:57:58 +1000105void qxl_reinit_memslots(struct qxl_device *qdev)
106{
107 setup_hw_slot(qdev, qdev->main_mem_slot, &qdev->mem_slots[qdev->main_mem_slot]);
108 setup_hw_slot(qdev, qdev->surfaces_mem_slot, &qdev->mem_slots[qdev->surfaces_mem_slot]);
109}
110
Dave Airlief64122c2013-02-25 14:47:55 +1000111static void qxl_gc_work(struct work_struct *work)
112{
113 struct qxl_device *qdev = container_of(work, struct qxl_device, gc_work);
114 qxl_garbage_collect(qdev);
115}
116
117int qxl_device_init(struct qxl_device *qdev,
118 struct drm_device *ddev,
119 struct pci_dev *pdev,
120 unsigned long flags)
121{
122 int r;
123
124 qdev->dev = &pdev->dev;
125 qdev->ddev = ddev;
126 qdev->pdev = pdev;
127 qdev->flags = flags;
128
129 mutex_init(&qdev->gem.mutex);
130 mutex_init(&qdev->update_area_mutex);
131 mutex_init(&qdev->release_mutex);
132 mutex_init(&qdev->surf_evict_mutex);
133 INIT_LIST_HEAD(&qdev->gem.objects);
134
135 qdev->rom_base = pci_resource_start(pdev, 2);
136 qdev->rom_size = pci_resource_len(pdev, 2);
137 qdev->vram_base = pci_resource_start(pdev, 0);
138 qdev->surfaceram_base = pci_resource_start(pdev, 1);
139 qdev->surfaceram_size = pci_resource_len(pdev, 1);
140 qdev->io_base = pci_resource_start(pdev, 3);
141
142 qdev->vram_mapping = io_mapping_create_wc(qdev->vram_base, pci_resource_len(pdev, 0));
143 qdev->surface_mapping = io_mapping_create_wc(qdev->surfaceram_base, qdev->surfaceram_size);
Dave Airlie970fa982013-05-31 12:45:09 +1000144 DRM_DEBUG_KMS("qxl: vram %llx-%llx(%dM %dk), surface %llx-%llx(%dM %dk)\n",
145 (unsigned long long)qdev->vram_base,
146 (unsigned long long)pci_resource_end(pdev, 0),
Dave Airlief64122c2013-02-25 14:47:55 +1000147 (int)pci_resource_len(pdev, 0) / 1024 / 1024,
148 (int)pci_resource_len(pdev, 0) / 1024,
Dave Airlie970fa982013-05-31 12:45:09 +1000149 (unsigned long long)qdev->surfaceram_base,
150 (unsigned long long)pci_resource_end(pdev, 1),
Dave Airlief64122c2013-02-25 14:47:55 +1000151 (int)qdev->surfaceram_size / 1024 / 1024,
152 (int)qdev->surfaceram_size / 1024);
153
154 qdev->rom = ioremap(qdev->rom_base, qdev->rom_size);
155 if (!qdev->rom) {
156 pr_err("Unable to ioremap ROM\n");
157 return -ENOMEM;
158 }
159
160 qxl_check_device(qdev);
161
162 r = qxl_bo_init(qdev);
163 if (r) {
164 DRM_ERROR("bo init failed %d\n", r);
165 return r;
166 }
167
168 qdev->ram_header = ioremap(qdev->vram_base +
169 qdev->rom->ram_header_offset,
170 sizeof(*qdev->ram_header));
171
172 qdev->command_ring = qxl_ring_create(&(qdev->ram_header->cmd_ring_hdr),
173 sizeof(struct qxl_command),
174 QXL_COMMAND_RING_SIZE,
175 qdev->io_base + QXL_IO_NOTIFY_CMD,
176 false,
177 &qdev->display_event);
178
179 qdev->cursor_ring = qxl_ring_create(
180 &(qdev->ram_header->cursor_ring_hdr),
181 sizeof(struct qxl_command),
182 QXL_CURSOR_RING_SIZE,
183 qdev->io_base + QXL_IO_NOTIFY_CMD,
184 false,
185 &qdev->cursor_event);
186
187 qdev->release_ring = qxl_ring_create(
188 &(qdev->ram_header->release_ring_hdr),
189 sizeof(uint64_t),
190 QXL_RELEASE_RING_SIZE, 0, true,
191 NULL);
192
193 /* TODO - slot initialization should happen on reset. where is our
194 * reset handler? */
195 qdev->n_mem_slots = qdev->rom->slots_end;
196 qdev->slot_gen_bits = qdev->rom->slot_gen_bits;
197 qdev->slot_id_bits = qdev->rom->slot_id_bits;
198 qdev->va_slot_mask =
199 (~(uint64_t)0) >> (qdev->slot_id_bits + qdev->slot_gen_bits);
200
201 qdev->mem_slots =
202 kmalloc(qdev->n_mem_slots * sizeof(struct qxl_memslot),
203 GFP_KERNEL);
204
205 idr_init(&qdev->release_idr);
206 spin_lock_init(&qdev->release_idr_lock);
207
208 idr_init(&qdev->surf_id_idr);
209 spin_lock_init(&qdev->surf_id_idr_lock);
210
211 mutex_init(&qdev->async_io_mutex);
212
213 /* reset the device into a known state - no memslots, no primary
214 * created, no surfaces. */
215 qxl_io_reset(qdev);
216
217 /* must initialize irq before first async io - slot creation */
218 r = qxl_irq_init(qdev);
219 if (r)
220 return r;
221
222 /*
223 * Note that virtual is surface0. We rely on the single ioremap done
224 * before.
225 */
226 qdev->main_mem_slot = setup_slot(qdev, 0,
227 (unsigned long)qdev->vram_base,
228 (unsigned long)qdev->vram_base + qdev->rom->ram_header_offset);
229 qdev->surfaces_mem_slot = setup_slot(qdev, 1,
230 (unsigned long)qdev->surfaceram_base,
231 (unsigned long)qdev->surfaceram_base + qdev->surfaceram_size);
232 DRM_INFO("main mem slot %d [%lx,%x)\n",
233 qdev->main_mem_slot,
234 (unsigned long)qdev->vram_base, qdev->rom->ram_header_offset);
235
236
237 qdev->gc_queue = create_singlethread_workqueue("qxl_gc");
238 INIT_WORK(&qdev->gc_work, qxl_gc_work);
239
240 r = qxl_fb_init(qdev);
241 if (r)
242 return r;
243
244 return 0;
245}
246
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000247static void qxl_device_fini(struct qxl_device *qdev)
Dave Airlief64122c2013-02-25 14:47:55 +1000248{
249 if (qdev->current_release_bo[0])
250 qxl_bo_unref(&qdev->current_release_bo[0]);
251 if (qdev->current_release_bo[1])
252 qxl_bo_unref(&qdev->current_release_bo[1]);
253 flush_workqueue(qdev->gc_queue);
254 destroy_workqueue(qdev->gc_queue);
255 qdev->gc_queue = NULL;
256
257 qxl_ring_free(qdev->command_ring);
258 qxl_ring_free(qdev->cursor_ring);
259 qxl_ring_free(qdev->release_ring);
260 qxl_bo_fini(qdev);
261 io_mapping_free(qdev->surface_mapping);
262 io_mapping_free(qdev->vram_mapping);
263 iounmap(qdev->ram_header);
264 iounmap(qdev->rom);
265 qdev->rom = NULL;
266 qdev->mode_info.modes = NULL;
267 qdev->mode_info.num_modes = 0;
268 qxl_debugfs_remove_files(qdev);
269}
270
271int qxl_driver_unload(struct drm_device *dev)
272{
273 struct qxl_device *qdev = dev->dev_private;
274
275 if (qdev == NULL)
276 return 0;
277 qxl_modeset_fini(qdev);
278 qxl_device_fini(qdev);
279
280 kfree(qdev);
281 dev->dev_private = NULL;
282 return 0;
283}
284
285int qxl_driver_load(struct drm_device *dev, unsigned long flags)
286{
287 struct qxl_device *qdev;
288 int r;
289
290 /* require kms */
291 if (!drm_core_check_feature(dev, DRIVER_MODESET))
292 return -ENODEV;
293
294 qdev = kzalloc(sizeof(struct qxl_device), GFP_KERNEL);
295 if (qdev == NULL)
296 return -ENOMEM;
297
298 dev->dev_private = qdev;
299
300 r = qxl_device_init(qdev, dev, dev->pdev, flags);
301 if (r)
302 goto out;
303
304 r = qxl_modeset_init(qdev);
305 if (r) {
306 qxl_driver_unload(dev);
307 goto out;
308 }
309
310 return 0;
311out:
312 kfree(qdev);
313 return r;
314}
315
316