2 * Copyright 2013 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * Authors: Dave Airlie
27 #include "qxl_object.h"
29 #include <drm/drm_crtc_helper.h>
30 #include <linux/io-mapping.h>
34 static void qxl_dump_mode(struct qxl_device
*qdev
, void *p
)
36 struct qxl_mode
*m
= p
;
37 DRM_DEBUG_KMS("%d: %dx%d %d bits, stride %d, %dmm x %dmm, orientation %d\n",
38 m
->id
, m
->x_res
, m
->y_res
, m
->bits
, m
->stride
, m
->x_mili
,
39 m
->y_mili
, m
->orientation
);
42 static bool qxl_check_device(struct qxl_device
*qdev
)
44 struct qxl_rom
*rom
= qdev
->rom
;
48 if (rom
->magic
!= 0x4f525851) {
49 DRM_ERROR("bad rom signature %x\n", rom
->magic
);
53 DRM_INFO("Device Version %d.%d\n", rom
->id
, rom
->update_id
);
54 DRM_INFO("Compression level %d log level %d\n", rom
->compression_level
,
56 DRM_INFO("Currently using mode #%d, list at 0x%x\n",
57 rom
->mode
, rom
->modes_offset
);
58 DRM_INFO("%d io pages at offset 0x%x\n",
59 rom
->num_io_pages
, rom
->pages_offset
);
60 DRM_INFO("%d byte draw area at offset 0x%x\n",
61 rom
->surface0_area_size
, rom
->draw_area_offset
);
63 qdev
->vram_size
= rom
->surface0_area_size
;
64 DRM_INFO("RAM header offset: 0x%x\n", rom
->ram_header_offset
);
66 mode_offset
= rom
->modes_offset
/ 4;
67 qdev
->mode_info
.num_modes
= ((u32
*)rom
)[mode_offset
];
68 DRM_INFO("rom modes offset 0x%x for %d modes\n", rom
->modes_offset
,
69 qdev
->mode_info
.num_modes
);
70 qdev
->mode_info
.modes
= (void *)((uint32_t *)rom
+ mode_offset
+ 1);
71 for (i
= 0; i
< qdev
->mode_info
.num_modes
; i
++)
72 qxl_dump_mode(qdev
, qdev
->mode_info
.modes
+ i
);
76 static void setup_hw_slot(struct qxl_device
*qdev
, int slot_index
,
77 struct qxl_memslot
*slot
)
79 qdev
->ram_header
->mem_slot
.mem_start
= slot
->start_phys_addr
;
80 qdev
->ram_header
->mem_slot
.mem_end
= slot
->end_phys_addr
;
81 qxl_io_memslot_add(qdev
, slot_index
);
84 static uint8_t setup_slot(struct qxl_device
*qdev
, uint8_t slot_index_offset
,
85 unsigned long start_phys_addr
, unsigned long end_phys_addr
)
88 struct qxl_memslot
*slot
;
91 slot_index
= qdev
->rom
->slots_start
+ slot_index_offset
;
92 slot
= &qdev
->mem_slots
[slot_index
];
93 slot
->start_phys_addr
= start_phys_addr
;
94 slot
->end_phys_addr
= end_phys_addr
;
96 setup_hw_slot(qdev
, slot_index
, slot
);
98 slot
->generation
= qdev
->rom
->slot_generation
;
99 high_bits
= slot_index
<< qdev
->slot_gen_bits
;
100 high_bits
|= slot
->generation
;
101 high_bits
<<= (64 - (qdev
->slot_gen_bits
+ qdev
->slot_id_bits
));
102 slot
->high_bits
= high_bits
;
106 void qxl_reinit_memslots(struct qxl_device
*qdev
)
108 setup_hw_slot(qdev
, qdev
->main_mem_slot
, &qdev
->mem_slots
[qdev
->main_mem_slot
]);
109 setup_hw_slot(qdev
, qdev
->surfaces_mem_slot
, &qdev
->mem_slots
[qdev
->surfaces_mem_slot
]);
112 static void qxl_gc_work(struct work_struct
*work
)
114 struct qxl_device
*qdev
= container_of(work
, struct qxl_device
, gc_work
);
115 qxl_garbage_collect(qdev
);
118 int qxl_device_init(struct qxl_device
*qdev
,
119 struct drm_device
*ddev
,
120 struct pci_dev
*pdev
,
125 qdev
->dev
= &pdev
->dev
;
130 mutex_init(&qdev
->gem
.mutex
);
131 mutex_init(&qdev
->update_area_mutex
);
132 mutex_init(&qdev
->release_mutex
);
133 mutex_init(&qdev
->surf_evict_mutex
);
134 INIT_LIST_HEAD(&qdev
->gem
.objects
);
136 qdev
->rom_base
= pci_resource_start(pdev
, 2);
137 qdev
->rom_size
= pci_resource_len(pdev
, 2);
138 qdev
->vram_base
= pci_resource_start(pdev
, 0);
139 qdev
->surfaceram_base
= pci_resource_start(pdev
, 1);
140 qdev
->surfaceram_size
= pci_resource_len(pdev
, 1);
141 qdev
->io_base
= pci_resource_start(pdev
, 3);
143 qdev
->vram_mapping
= io_mapping_create_wc(qdev
->vram_base
, pci_resource_len(pdev
, 0));
144 qdev
->surface_mapping
= io_mapping_create_wc(qdev
->surfaceram_base
, qdev
->surfaceram_size
);
145 DRM_DEBUG_KMS("qxl: vram %llx-%llx(%dM %dk), surface %llx-%llx(%dM %dk)\n",
146 (unsigned long long)qdev
->vram_base
,
147 (unsigned long long)pci_resource_end(pdev
, 0),
148 (int)pci_resource_len(pdev
, 0) / 1024 / 1024,
149 (int)pci_resource_len(pdev
, 0) / 1024,
150 (unsigned long long)qdev
->surfaceram_base
,
151 (unsigned long long)pci_resource_end(pdev
, 1),
152 (int)qdev
->surfaceram_size
/ 1024 / 1024,
153 (int)qdev
->surfaceram_size
/ 1024);
155 qdev
->rom
= ioremap(qdev
->rom_base
, qdev
->rom_size
);
157 pr_err("Unable to ioremap ROM\n");
161 qxl_check_device(qdev
);
163 r
= qxl_bo_init(qdev
);
165 DRM_ERROR("bo init failed %d\n", r
);
169 qdev
->ram_header
= ioremap(qdev
->vram_base
+
170 qdev
->rom
->ram_header_offset
,
171 sizeof(*qdev
->ram_header
));
173 qdev
->command_ring
= qxl_ring_create(&(qdev
->ram_header
->cmd_ring_hdr
),
174 sizeof(struct qxl_command
),
175 QXL_COMMAND_RING_SIZE
,
176 qdev
->io_base
+ QXL_IO_NOTIFY_CMD
,
178 &qdev
->display_event
);
180 qdev
->cursor_ring
= qxl_ring_create(
181 &(qdev
->ram_header
->cursor_ring_hdr
),
182 sizeof(struct qxl_command
),
183 QXL_CURSOR_RING_SIZE
,
184 qdev
->io_base
+ QXL_IO_NOTIFY_CMD
,
186 &qdev
->cursor_event
);
188 qdev
->release_ring
= qxl_ring_create(
189 &(qdev
->ram_header
->release_ring_hdr
),
191 QXL_RELEASE_RING_SIZE
, 0, true,
194 /* TODO - slot initialization should happen on reset. where is our
196 qdev
->n_mem_slots
= qdev
->rom
->slots_end
;
197 qdev
->slot_gen_bits
= qdev
->rom
->slot_gen_bits
;
198 qdev
->slot_id_bits
= qdev
->rom
->slot_id_bits
;
200 (~(uint64_t)0) >> (qdev
->slot_id_bits
+ qdev
->slot_gen_bits
);
203 kmalloc(qdev
->n_mem_slots
* sizeof(struct qxl_memslot
),
206 idr_init(&qdev
->release_idr
);
207 spin_lock_init(&qdev
->release_idr_lock
);
209 idr_init(&qdev
->surf_id_idr
);
210 spin_lock_init(&qdev
->surf_id_idr_lock
);
212 mutex_init(&qdev
->async_io_mutex
);
214 /* reset the device into a known state - no memslots, no primary
215 * created, no surfaces. */
218 /* must initialize irq before first async io - slot creation */
219 r
= qxl_irq_init(qdev
);
224 * Note that virtual is surface0. We rely on the single ioremap done
227 qdev
->main_mem_slot
= setup_slot(qdev
, 0,
228 (unsigned long)qdev
->vram_base
,
229 (unsigned long)qdev
->vram_base
+ qdev
->rom
->ram_header_offset
);
230 qdev
->surfaces_mem_slot
= setup_slot(qdev
, 1,
231 (unsigned long)qdev
->surfaceram_base
,
232 (unsigned long)qdev
->surfaceram_base
+ qdev
->surfaceram_size
);
233 DRM_INFO("main mem slot %d [%lx,%x)\n",
235 (unsigned long)qdev
->vram_base
, qdev
->rom
->ram_header_offset
);
238 qdev
->gc_queue
= create_singlethread_workqueue("qxl_gc");
239 INIT_WORK(&qdev
->gc_work
, qxl_gc_work
);
241 r
= qxl_fb_init(qdev
);
248 static void qxl_device_fini(struct qxl_device
*qdev
)
250 if (qdev
->current_release_bo
[0])
251 qxl_bo_unref(&qdev
->current_release_bo
[0]);
252 if (qdev
->current_release_bo
[1])
253 qxl_bo_unref(&qdev
->current_release_bo
[1]);
254 flush_workqueue(qdev
->gc_queue
);
255 destroy_workqueue(qdev
->gc_queue
);
256 qdev
->gc_queue
= NULL
;
258 qxl_ring_free(qdev
->command_ring
);
259 qxl_ring_free(qdev
->cursor_ring
);
260 qxl_ring_free(qdev
->release_ring
);
262 io_mapping_free(qdev
->surface_mapping
);
263 io_mapping_free(qdev
->vram_mapping
);
264 iounmap(qdev
->ram_header
);
267 qdev
->mode_info
.modes
= NULL
;
268 qdev
->mode_info
.num_modes
= 0;
269 qxl_debugfs_remove_files(qdev
);
272 int qxl_driver_unload(struct drm_device
*dev
)
274 struct qxl_device
*qdev
= dev
->dev_private
;
278 qxl_modeset_fini(qdev
);
279 qxl_device_fini(qdev
);
282 dev
->dev_private
= NULL
;
286 int qxl_driver_load(struct drm_device
*dev
, unsigned long flags
)
288 struct qxl_device
*qdev
;
292 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
295 qdev
= kzalloc(sizeof(struct qxl_device
), GFP_KERNEL
);
299 dev
->dev_private
= qdev
;
301 r
= qxl_device_init(qdev
, dev
, dev
->pdev
, flags
);
305 r
= qxl_modeset_init(qdev
);
307 qxl_driver_unload(dev
);
311 drm_kms_helper_poll_init(qdev
->ddev
);