1*f64122c1SDave Airlie /* 2*f64122c1SDave Airlie * Copyright 2013 Red Hat Inc. 3*f64122c1SDave Airlie * 4*f64122c1SDave Airlie * Permission is hereby granted, free of charge, to any person obtaining a 5*f64122c1SDave Airlie * copy of this software and associated documentation files (the "Software"), 6*f64122c1SDave Airlie * to deal in the Software without restriction, including without limitation 7*f64122c1SDave Airlie * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8*f64122c1SDave Airlie * and/or sell copies of the Software, and to permit persons to whom the 9*f64122c1SDave Airlie * Software is furnished to do so, subject to the following conditions: 10*f64122c1SDave Airlie * 11*f64122c1SDave Airlie * The above copyright notice and this permission notice shall be included in 12*f64122c1SDave Airlie * all copies or substantial portions of the Software. 13*f64122c1SDave Airlie * 14*f64122c1SDave Airlie * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15*f64122c1SDave Airlie * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16*f64122c1SDave Airlie * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17*f64122c1SDave Airlie * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18*f64122c1SDave Airlie * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19*f64122c1SDave Airlie * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20*f64122c1SDave Airlie * OTHER DEALINGS IN THE SOFTWARE. 21*f64122c1SDave Airlie * 22*f64122c1SDave Airlie * Authors: Dave Airlie 23*f64122c1SDave Airlie * Alon Levy 24*f64122c1SDave Airlie */ 25*f64122c1SDave Airlie 26*f64122c1SDave Airlie /* QXL cmd/ring handling */ 27*f64122c1SDave Airlie 28*f64122c1SDave Airlie #include "qxl_drv.h" 29*f64122c1SDave Airlie #include "qxl_object.h" 30*f64122c1SDave Airlie 31*f64122c1SDave Airlie static int qxl_reap_surface_id(struct qxl_device *qdev, int max_to_reap); 32*f64122c1SDave Airlie 33*f64122c1SDave Airlie struct ring { 34*f64122c1SDave Airlie struct qxl_ring_header header; 35*f64122c1SDave Airlie uint8_t elements[0]; 36*f64122c1SDave Airlie }; 37*f64122c1SDave Airlie 38*f64122c1SDave Airlie struct qxl_ring { 39*f64122c1SDave Airlie struct ring *ring; 40*f64122c1SDave Airlie int element_size; 41*f64122c1SDave Airlie int n_elements; 42*f64122c1SDave Airlie int prod_notify; 43*f64122c1SDave Airlie wait_queue_head_t *push_event; 44*f64122c1SDave Airlie spinlock_t lock; 45*f64122c1SDave Airlie }; 46*f64122c1SDave Airlie 47*f64122c1SDave Airlie void qxl_ring_free(struct qxl_ring *ring) 48*f64122c1SDave Airlie { 49*f64122c1SDave Airlie kfree(ring); 50*f64122c1SDave Airlie } 51*f64122c1SDave Airlie 52*f64122c1SDave Airlie struct qxl_ring * 53*f64122c1SDave Airlie qxl_ring_create(struct qxl_ring_header *header, 54*f64122c1SDave Airlie int element_size, 55*f64122c1SDave Airlie int n_elements, 56*f64122c1SDave Airlie int prod_notify, 57*f64122c1SDave Airlie bool set_prod_notify, 58*f64122c1SDave Airlie wait_queue_head_t *push_event) 59*f64122c1SDave Airlie { 60*f64122c1SDave Airlie struct qxl_ring *ring; 61*f64122c1SDave Airlie 62*f64122c1SDave Airlie ring = kmalloc(sizeof(*ring), GFP_KERNEL); 63*f64122c1SDave Airlie if (!ring) 64*f64122c1SDave Airlie return NULL; 65*f64122c1SDave Airlie 66*f64122c1SDave Airlie ring->ring = (struct ring *)header; 67*f64122c1SDave Airlie ring->element_size = element_size; 68*f64122c1SDave Airlie ring->n_elements = n_elements; 69*f64122c1SDave Airlie ring->prod_notify = prod_notify; 70*f64122c1SDave Airlie ring->push_event = push_event; 71*f64122c1SDave Airlie if (set_prod_notify) 72*f64122c1SDave Airlie header->notify_on_prod = ring->n_elements; 73*f64122c1SDave Airlie spin_lock_init(&ring->lock); 74*f64122c1SDave Airlie return ring; 75*f64122c1SDave Airlie } 76*f64122c1SDave Airlie 77*f64122c1SDave Airlie static int qxl_check_header(struct qxl_ring *ring) 78*f64122c1SDave Airlie { 79*f64122c1SDave Airlie int ret; 80*f64122c1SDave Airlie struct qxl_ring_header *header = &(ring->ring->header); 81*f64122c1SDave Airlie unsigned long flags; 82*f64122c1SDave Airlie spin_lock_irqsave(&ring->lock, flags); 83*f64122c1SDave Airlie ret = header->prod - header->cons < header->num_items; 84*f64122c1SDave Airlie if (ret == 0) 85*f64122c1SDave Airlie header->notify_on_cons = header->cons + 1; 86*f64122c1SDave Airlie spin_unlock_irqrestore(&ring->lock, flags); 87*f64122c1SDave Airlie return ret; 88*f64122c1SDave Airlie } 89*f64122c1SDave Airlie 90*f64122c1SDave Airlie static int qxl_check_idle(struct qxl_ring *ring) 91*f64122c1SDave Airlie { 92*f64122c1SDave Airlie int ret; 93*f64122c1SDave Airlie struct qxl_ring_header *header = &(ring->ring->header); 94*f64122c1SDave Airlie unsigned long flags; 95*f64122c1SDave Airlie spin_lock_irqsave(&ring->lock, flags); 96*f64122c1SDave Airlie ret = header->prod == header->cons; 97*f64122c1SDave Airlie spin_unlock_irqrestore(&ring->lock, flags); 98*f64122c1SDave Airlie return ret; 99*f64122c1SDave Airlie } 100*f64122c1SDave Airlie 101*f64122c1SDave Airlie int qxl_ring_push(struct qxl_ring *ring, 102*f64122c1SDave Airlie const void *new_elt, bool interruptible) 103*f64122c1SDave Airlie { 104*f64122c1SDave Airlie struct qxl_ring_header *header = &(ring->ring->header); 105*f64122c1SDave Airlie uint8_t *elt; 106*f64122c1SDave Airlie int idx, ret; 107*f64122c1SDave Airlie unsigned long flags; 108*f64122c1SDave Airlie spin_lock_irqsave(&ring->lock, flags); 109*f64122c1SDave Airlie if (header->prod - header->cons == header->num_items) { 110*f64122c1SDave Airlie header->notify_on_cons = header->cons + 1; 111*f64122c1SDave Airlie mb(); 112*f64122c1SDave Airlie spin_unlock_irqrestore(&ring->lock, flags); 113*f64122c1SDave Airlie if (!drm_can_sleep()) { 114*f64122c1SDave Airlie while (!qxl_check_header(ring)) 115*f64122c1SDave Airlie udelay(1); 116*f64122c1SDave Airlie } else { 117*f64122c1SDave Airlie if (interruptible) { 118*f64122c1SDave Airlie ret = wait_event_interruptible(*ring->push_event, 119*f64122c1SDave Airlie qxl_check_header(ring)); 120*f64122c1SDave Airlie if (ret) 121*f64122c1SDave Airlie return ret; 122*f64122c1SDave Airlie } else { 123*f64122c1SDave Airlie wait_event(*ring->push_event, 124*f64122c1SDave Airlie qxl_check_header(ring)); 125*f64122c1SDave Airlie } 126*f64122c1SDave Airlie 127*f64122c1SDave Airlie } 128*f64122c1SDave Airlie spin_lock_irqsave(&ring->lock, flags); 129*f64122c1SDave Airlie } 130*f64122c1SDave Airlie 131*f64122c1SDave Airlie idx = header->prod & (ring->n_elements - 1); 132*f64122c1SDave Airlie elt = ring->ring->elements + idx * ring->element_size; 133*f64122c1SDave Airlie 134*f64122c1SDave Airlie memcpy((void *)elt, new_elt, ring->element_size); 135*f64122c1SDave Airlie 136*f64122c1SDave Airlie header->prod++; 137*f64122c1SDave Airlie 138*f64122c1SDave Airlie mb(); 139*f64122c1SDave Airlie 140*f64122c1SDave Airlie if (header->prod == header->notify_on_prod) 141*f64122c1SDave Airlie outb(0, ring->prod_notify); 142*f64122c1SDave Airlie 143*f64122c1SDave Airlie spin_unlock_irqrestore(&ring->lock, flags); 144*f64122c1SDave Airlie return 0; 145*f64122c1SDave Airlie } 146*f64122c1SDave Airlie 147*f64122c1SDave Airlie bool qxl_ring_pop(struct qxl_ring *ring, 148*f64122c1SDave Airlie void *element) 149*f64122c1SDave Airlie { 150*f64122c1SDave Airlie volatile struct qxl_ring_header *header = &(ring->ring->header); 151*f64122c1SDave Airlie volatile uint8_t *ring_elt; 152*f64122c1SDave Airlie int idx; 153*f64122c1SDave Airlie unsigned long flags; 154*f64122c1SDave Airlie spin_lock_irqsave(&ring->lock, flags); 155*f64122c1SDave Airlie if (header->cons == header->prod) { 156*f64122c1SDave Airlie header->notify_on_prod = header->cons + 1; 157*f64122c1SDave Airlie spin_unlock_irqrestore(&ring->lock, flags); 158*f64122c1SDave Airlie return false; 159*f64122c1SDave Airlie } 160*f64122c1SDave Airlie 161*f64122c1SDave Airlie idx = header->cons & (ring->n_elements - 1); 162*f64122c1SDave Airlie ring_elt = ring->ring->elements + idx * ring->element_size; 163*f64122c1SDave Airlie 164*f64122c1SDave Airlie memcpy(element, (void *)ring_elt, ring->element_size); 165*f64122c1SDave Airlie 166*f64122c1SDave Airlie header->cons++; 167*f64122c1SDave Airlie 168*f64122c1SDave Airlie spin_unlock_irqrestore(&ring->lock, flags); 169*f64122c1SDave Airlie return true; 170*f64122c1SDave Airlie } 171*f64122c1SDave Airlie 172*f64122c1SDave Airlie void qxl_ring_wait_idle(struct qxl_ring *ring) 173*f64122c1SDave Airlie { 174*f64122c1SDave Airlie struct qxl_ring_header *header = &(ring->ring->header); 175*f64122c1SDave Airlie unsigned long flags; 176*f64122c1SDave Airlie 177*f64122c1SDave Airlie spin_lock_irqsave(&ring->lock, flags); 178*f64122c1SDave Airlie if (ring->ring->header.cons < ring->ring->header.prod) { 179*f64122c1SDave Airlie header->notify_on_cons = header->prod; 180*f64122c1SDave Airlie mb(); 181*f64122c1SDave Airlie spin_unlock_irqrestore(&ring->lock, flags); 182*f64122c1SDave Airlie wait_event_interruptible(*ring->push_event, 183*f64122c1SDave Airlie qxl_check_idle(ring)); 184*f64122c1SDave Airlie spin_lock_irqsave(&ring->lock, flags); 185*f64122c1SDave Airlie } 186*f64122c1SDave Airlie spin_unlock_irqrestore(&ring->lock, flags); 187*f64122c1SDave Airlie } 188*f64122c1SDave Airlie 189*f64122c1SDave Airlie int 190*f64122c1SDave Airlie qxl_push_command_ring_release(struct qxl_device *qdev, struct qxl_release *release, 191*f64122c1SDave Airlie uint32_t type, bool interruptible) 192*f64122c1SDave Airlie { 193*f64122c1SDave Airlie struct qxl_command cmd; 194*f64122c1SDave Airlie 195*f64122c1SDave Airlie cmd.type = type; 196*f64122c1SDave Airlie cmd.data = qxl_bo_physical_address(qdev, release->bos[0], release->release_offset); 197*f64122c1SDave Airlie 198*f64122c1SDave Airlie return qxl_ring_push(qdev->command_ring, &cmd, interruptible); 199*f64122c1SDave Airlie } 200*f64122c1SDave Airlie 201*f64122c1SDave Airlie int 202*f64122c1SDave Airlie qxl_push_cursor_ring_release(struct qxl_device *qdev, struct qxl_release *release, 203*f64122c1SDave Airlie uint32_t type, bool interruptible) 204*f64122c1SDave Airlie { 205*f64122c1SDave Airlie struct qxl_command cmd; 206*f64122c1SDave Airlie 207*f64122c1SDave Airlie cmd.type = type; 208*f64122c1SDave Airlie cmd.data = qxl_bo_physical_address(qdev, release->bos[0], release->release_offset); 209*f64122c1SDave Airlie 210*f64122c1SDave Airlie return qxl_ring_push(qdev->cursor_ring, &cmd, interruptible); 211*f64122c1SDave Airlie } 212*f64122c1SDave Airlie 213*f64122c1SDave Airlie bool qxl_queue_garbage_collect(struct qxl_device *qdev, bool flush) 214*f64122c1SDave Airlie { 215*f64122c1SDave Airlie if (!qxl_check_idle(qdev->release_ring)) { 216*f64122c1SDave Airlie queue_work(qdev->gc_queue, &qdev->gc_work); 217*f64122c1SDave Airlie if (flush) 218*f64122c1SDave Airlie flush_work(&qdev->gc_work); 219*f64122c1SDave Airlie return true; 220*f64122c1SDave Airlie } 221*f64122c1SDave Airlie return false; 222*f64122c1SDave Airlie } 223*f64122c1SDave Airlie 224*f64122c1SDave Airlie int qxl_garbage_collect(struct qxl_device *qdev) 225*f64122c1SDave Airlie { 226*f64122c1SDave Airlie struct qxl_release *release; 227*f64122c1SDave Airlie uint64_t id, next_id; 228*f64122c1SDave Airlie int i = 0; 229*f64122c1SDave Airlie int ret; 230*f64122c1SDave Airlie union qxl_release_info *info; 231*f64122c1SDave Airlie 232*f64122c1SDave Airlie while (qxl_ring_pop(qdev->release_ring, &id)) { 233*f64122c1SDave Airlie QXL_INFO(qdev, "popped %lld\n", id); 234*f64122c1SDave Airlie while (id) { 235*f64122c1SDave Airlie release = qxl_release_from_id_locked(qdev, id); 236*f64122c1SDave Airlie if (release == NULL) 237*f64122c1SDave Airlie break; 238*f64122c1SDave Airlie 239*f64122c1SDave Airlie ret = qxl_release_reserve(qdev, release, false); 240*f64122c1SDave Airlie if (ret) { 241*f64122c1SDave Airlie qxl_io_log(qdev, "failed to reserve release on garbage collect %lld\n", id); 242*f64122c1SDave Airlie DRM_ERROR("failed to reserve release %lld\n", id); 243*f64122c1SDave Airlie } 244*f64122c1SDave Airlie 245*f64122c1SDave Airlie info = qxl_release_map(qdev, release); 246*f64122c1SDave Airlie next_id = info->next; 247*f64122c1SDave Airlie qxl_release_unmap(qdev, release, info); 248*f64122c1SDave Airlie 249*f64122c1SDave Airlie qxl_release_unreserve(qdev, release); 250*f64122c1SDave Airlie QXL_INFO(qdev, "popped %lld, next %lld\n", id, 251*f64122c1SDave Airlie next_id); 252*f64122c1SDave Airlie 253*f64122c1SDave Airlie switch (release->type) { 254*f64122c1SDave Airlie case QXL_RELEASE_DRAWABLE: 255*f64122c1SDave Airlie case QXL_RELEASE_SURFACE_CMD: 256*f64122c1SDave Airlie case QXL_RELEASE_CURSOR_CMD: 257*f64122c1SDave Airlie break; 258*f64122c1SDave Airlie default: 259*f64122c1SDave Airlie DRM_ERROR("unexpected release type\n"); 260*f64122c1SDave Airlie break; 261*f64122c1SDave Airlie } 262*f64122c1SDave Airlie id = next_id; 263*f64122c1SDave Airlie 264*f64122c1SDave Airlie qxl_release_free(qdev, release); 265*f64122c1SDave Airlie ++i; 266*f64122c1SDave Airlie } 267*f64122c1SDave Airlie } 268*f64122c1SDave Airlie 269*f64122c1SDave Airlie QXL_INFO(qdev, "%s: %lld\n", __func__, i); 270*f64122c1SDave Airlie 271*f64122c1SDave Airlie return i; 272*f64122c1SDave Airlie } 273*f64122c1SDave Airlie 274*f64122c1SDave Airlie int qxl_alloc_bo_reserved(struct qxl_device *qdev, unsigned long size, 275*f64122c1SDave Airlie struct qxl_bo **_bo) 276*f64122c1SDave Airlie { 277*f64122c1SDave Airlie struct qxl_bo *bo; 278*f64122c1SDave Airlie int ret; 279*f64122c1SDave Airlie 280*f64122c1SDave Airlie ret = qxl_bo_create(qdev, size, false /* not kernel - device */, 281*f64122c1SDave Airlie QXL_GEM_DOMAIN_VRAM, NULL, &bo); 282*f64122c1SDave Airlie if (ret) { 283*f64122c1SDave Airlie DRM_ERROR("failed to allocate VRAM BO\n"); 284*f64122c1SDave Airlie return ret; 285*f64122c1SDave Airlie } 286*f64122c1SDave Airlie ret = qxl_bo_reserve(bo, false); 287*f64122c1SDave Airlie if (unlikely(ret != 0)) 288*f64122c1SDave Airlie goto out_unref; 289*f64122c1SDave Airlie 290*f64122c1SDave Airlie *_bo = bo; 291*f64122c1SDave Airlie return 0; 292*f64122c1SDave Airlie out_unref: 293*f64122c1SDave Airlie qxl_bo_unref(&bo); 294*f64122c1SDave Airlie return 0; 295*f64122c1SDave Airlie } 296*f64122c1SDave Airlie 297*f64122c1SDave Airlie static int wait_for_io_cmd_user(struct qxl_device *qdev, uint8_t val, long port) 298*f64122c1SDave Airlie { 299*f64122c1SDave Airlie int irq_num; 300*f64122c1SDave Airlie long addr = qdev->io_base + port; 301*f64122c1SDave Airlie int ret; 302*f64122c1SDave Airlie 303*f64122c1SDave Airlie mutex_lock(&qdev->async_io_mutex); 304*f64122c1SDave Airlie irq_num = atomic_read(&qdev->irq_received_io_cmd); 305*f64122c1SDave Airlie 306*f64122c1SDave Airlie 307*f64122c1SDave Airlie if (qdev->last_sent_io_cmd > irq_num) { 308*f64122c1SDave Airlie ret = wait_event_interruptible(qdev->io_cmd_event, 309*f64122c1SDave Airlie atomic_read(&qdev->irq_received_io_cmd) > irq_num); 310*f64122c1SDave Airlie if (ret) 311*f64122c1SDave Airlie goto out; 312*f64122c1SDave Airlie irq_num = atomic_read(&qdev->irq_received_io_cmd); 313*f64122c1SDave Airlie } 314*f64122c1SDave Airlie outb(val, addr); 315*f64122c1SDave Airlie qdev->last_sent_io_cmd = irq_num + 1; 316*f64122c1SDave Airlie ret = wait_event_interruptible(qdev->io_cmd_event, 317*f64122c1SDave Airlie atomic_read(&qdev->irq_received_io_cmd) > irq_num); 318*f64122c1SDave Airlie out: 319*f64122c1SDave Airlie mutex_unlock(&qdev->async_io_mutex); 320*f64122c1SDave Airlie return ret; 321*f64122c1SDave Airlie } 322*f64122c1SDave Airlie 323*f64122c1SDave Airlie static void wait_for_io_cmd(struct qxl_device *qdev, uint8_t val, long port) 324*f64122c1SDave Airlie { 325*f64122c1SDave Airlie int ret; 326*f64122c1SDave Airlie 327*f64122c1SDave Airlie restart: 328*f64122c1SDave Airlie ret = wait_for_io_cmd_user(qdev, val, port); 329*f64122c1SDave Airlie if (ret == -ERESTARTSYS) 330*f64122c1SDave Airlie goto restart; 331*f64122c1SDave Airlie } 332*f64122c1SDave Airlie 333*f64122c1SDave Airlie int qxl_io_update_area(struct qxl_device *qdev, struct qxl_bo *surf, 334*f64122c1SDave Airlie const struct qxl_rect *area) 335*f64122c1SDave Airlie { 336*f64122c1SDave Airlie int surface_id; 337*f64122c1SDave Airlie uint32_t surface_width, surface_height; 338*f64122c1SDave Airlie int ret; 339*f64122c1SDave Airlie 340*f64122c1SDave Airlie if (!surf->hw_surf_alloc) 341*f64122c1SDave Airlie DRM_ERROR("got io update area with no hw surface\n"); 342*f64122c1SDave Airlie 343*f64122c1SDave Airlie if (surf->is_primary) 344*f64122c1SDave Airlie surface_id = 0; 345*f64122c1SDave Airlie else 346*f64122c1SDave Airlie surface_id = surf->surface_id; 347*f64122c1SDave Airlie surface_width = surf->surf.width; 348*f64122c1SDave Airlie surface_height = surf->surf.height; 349*f64122c1SDave Airlie 350*f64122c1SDave Airlie if (area->left < 0 || area->top < 0 || 351*f64122c1SDave Airlie area->right > surface_width || area->bottom > surface_height) { 352*f64122c1SDave Airlie qxl_io_log(qdev, "%s: not doing area update for " 353*f64122c1SDave Airlie "%d, (%d,%d,%d,%d) (%d,%d)\n", __func__, surface_id, area->left, 354*f64122c1SDave Airlie area->top, area->right, area->bottom, surface_width, surface_height); 355*f64122c1SDave Airlie return -EINVAL; 356*f64122c1SDave Airlie } 357*f64122c1SDave Airlie mutex_lock(&qdev->update_area_mutex); 358*f64122c1SDave Airlie qdev->ram_header->update_area = *area; 359*f64122c1SDave Airlie qdev->ram_header->update_surface = surface_id; 360*f64122c1SDave Airlie ret = wait_for_io_cmd_user(qdev, 0, QXL_IO_UPDATE_AREA_ASYNC); 361*f64122c1SDave Airlie mutex_unlock(&qdev->update_area_mutex); 362*f64122c1SDave Airlie return ret; 363*f64122c1SDave Airlie } 364*f64122c1SDave Airlie 365*f64122c1SDave Airlie void qxl_io_notify_oom(struct qxl_device *qdev) 366*f64122c1SDave Airlie { 367*f64122c1SDave Airlie outb(0, qdev->io_base + QXL_IO_NOTIFY_OOM); 368*f64122c1SDave Airlie } 369*f64122c1SDave Airlie 370*f64122c1SDave Airlie void qxl_io_flush_release(struct qxl_device *qdev) 371*f64122c1SDave Airlie { 372*f64122c1SDave Airlie outb(0, qdev->io_base + QXL_IO_FLUSH_RELEASE); 373*f64122c1SDave Airlie } 374*f64122c1SDave Airlie 375*f64122c1SDave Airlie void qxl_io_flush_surfaces(struct qxl_device *qdev) 376*f64122c1SDave Airlie { 377*f64122c1SDave Airlie wait_for_io_cmd(qdev, 0, QXL_IO_FLUSH_SURFACES_ASYNC); 378*f64122c1SDave Airlie } 379*f64122c1SDave Airlie 380*f64122c1SDave Airlie 381*f64122c1SDave Airlie void qxl_io_destroy_primary(struct qxl_device *qdev) 382*f64122c1SDave Airlie { 383*f64122c1SDave Airlie wait_for_io_cmd(qdev, 0, QXL_IO_DESTROY_PRIMARY_ASYNC); 384*f64122c1SDave Airlie } 385*f64122c1SDave Airlie 386*f64122c1SDave Airlie void qxl_io_create_primary(struct qxl_device *qdev, unsigned width, 387*f64122c1SDave Airlie unsigned height, unsigned offset, struct qxl_bo *bo) 388*f64122c1SDave Airlie { 389*f64122c1SDave Airlie struct qxl_surface_create *create; 390*f64122c1SDave Airlie 391*f64122c1SDave Airlie QXL_INFO(qdev, "%s: qdev %p, ram_header %p\n", __func__, qdev, 392*f64122c1SDave Airlie qdev->ram_header); 393*f64122c1SDave Airlie create = &qdev->ram_header->create_surface; 394*f64122c1SDave Airlie create->format = bo->surf.format; 395*f64122c1SDave Airlie create->width = width; 396*f64122c1SDave Airlie create->height = height; 397*f64122c1SDave Airlie create->stride = bo->surf.stride; 398*f64122c1SDave Airlie create->mem = qxl_bo_physical_address(qdev, bo, offset); 399*f64122c1SDave Airlie 400*f64122c1SDave Airlie QXL_INFO(qdev, "%s: mem = %llx, from %p\n", __func__, create->mem, 401*f64122c1SDave Airlie bo->kptr); 402*f64122c1SDave Airlie 403*f64122c1SDave Airlie create->flags = QXL_SURF_FLAG_KEEP_DATA; 404*f64122c1SDave Airlie create->type = QXL_SURF_TYPE_PRIMARY; 405*f64122c1SDave Airlie 406*f64122c1SDave Airlie wait_for_io_cmd(qdev, 0, QXL_IO_CREATE_PRIMARY_ASYNC); 407*f64122c1SDave Airlie } 408*f64122c1SDave Airlie 409*f64122c1SDave Airlie void qxl_io_memslot_add(struct qxl_device *qdev, uint8_t id) 410*f64122c1SDave Airlie { 411*f64122c1SDave Airlie QXL_INFO(qdev, "qxl_memslot_add %d\n", id); 412*f64122c1SDave Airlie wait_for_io_cmd(qdev, id, QXL_IO_MEMSLOT_ADD_ASYNC); 413*f64122c1SDave Airlie } 414*f64122c1SDave Airlie 415*f64122c1SDave Airlie void qxl_io_log(struct qxl_device *qdev, const char *fmt, ...) 416*f64122c1SDave Airlie { 417*f64122c1SDave Airlie va_list args; 418*f64122c1SDave Airlie 419*f64122c1SDave Airlie va_start(args, fmt); 420*f64122c1SDave Airlie vsnprintf(qdev->ram_header->log_buf, QXL_LOG_BUF_SIZE, fmt, args); 421*f64122c1SDave Airlie va_end(args); 422*f64122c1SDave Airlie /* 423*f64122c1SDave Airlie * DO not do a DRM output here - this will call printk, which will 424*f64122c1SDave Airlie * call back into qxl for rendering (qxl_fb) 425*f64122c1SDave Airlie */ 426*f64122c1SDave Airlie outb(0, qdev->io_base + QXL_IO_LOG); 427*f64122c1SDave Airlie } 428*f64122c1SDave Airlie 429*f64122c1SDave Airlie void qxl_io_reset(struct qxl_device *qdev) 430*f64122c1SDave Airlie { 431*f64122c1SDave Airlie outb(0, qdev->io_base + QXL_IO_RESET); 432*f64122c1SDave Airlie } 433*f64122c1SDave Airlie 434*f64122c1SDave Airlie void qxl_io_monitors_config(struct qxl_device *qdev) 435*f64122c1SDave Airlie { 436*f64122c1SDave Airlie qxl_io_log(qdev, "%s: %d [%dx%d+%d+%d]\n", __func__, 437*f64122c1SDave Airlie qdev->monitors_config ? 438*f64122c1SDave Airlie qdev->monitors_config->count : -1, 439*f64122c1SDave Airlie qdev->monitors_config && qdev->monitors_config->count ? 440*f64122c1SDave Airlie qdev->monitors_config->heads[0].width : -1, 441*f64122c1SDave Airlie qdev->monitors_config && qdev->monitors_config->count ? 442*f64122c1SDave Airlie qdev->monitors_config->heads[0].height : -1, 443*f64122c1SDave Airlie qdev->monitors_config && qdev->monitors_config->count ? 444*f64122c1SDave Airlie qdev->monitors_config->heads[0].x : -1, 445*f64122c1SDave Airlie qdev->monitors_config && qdev->monitors_config->count ? 446*f64122c1SDave Airlie qdev->monitors_config->heads[0].y : -1 447*f64122c1SDave Airlie ); 448*f64122c1SDave Airlie 449*f64122c1SDave Airlie wait_for_io_cmd(qdev, 0, QXL_IO_MONITORS_CONFIG_ASYNC); 450*f64122c1SDave Airlie } 451*f64122c1SDave Airlie 452*f64122c1SDave Airlie int qxl_surface_id_alloc(struct qxl_device *qdev, 453*f64122c1SDave Airlie struct qxl_bo *surf) 454*f64122c1SDave Airlie { 455*f64122c1SDave Airlie uint32_t handle = -ENOMEM; 456*f64122c1SDave Airlie int idr_ret; 457*f64122c1SDave Airlie int count = 0; 458*f64122c1SDave Airlie again: 459*f64122c1SDave Airlie if (idr_pre_get(&qdev->surf_id_idr, GFP_ATOMIC) == 0) { 460*f64122c1SDave Airlie DRM_ERROR("Out of memory for surf idr\n"); 461*f64122c1SDave Airlie kfree(surf); 462*f64122c1SDave Airlie goto alloc_fail; 463*f64122c1SDave Airlie } 464*f64122c1SDave Airlie 465*f64122c1SDave Airlie spin_lock(&qdev->surf_id_idr_lock); 466*f64122c1SDave Airlie idr_ret = idr_get_new_above(&qdev->surf_id_idr, NULL, 1, &handle); 467*f64122c1SDave Airlie spin_unlock(&qdev->surf_id_idr_lock); 468*f64122c1SDave Airlie 469*f64122c1SDave Airlie if (idr_ret == -EAGAIN) 470*f64122c1SDave Airlie goto again; 471*f64122c1SDave Airlie 472*f64122c1SDave Airlie if (handle >= qdev->rom->n_surfaces) { 473*f64122c1SDave Airlie count++; 474*f64122c1SDave Airlie spin_lock(&qdev->surf_id_idr_lock); 475*f64122c1SDave Airlie idr_remove(&qdev->surf_id_idr, handle); 476*f64122c1SDave Airlie spin_unlock(&qdev->surf_id_idr_lock); 477*f64122c1SDave Airlie qxl_reap_surface_id(qdev, 2); 478*f64122c1SDave Airlie goto again; 479*f64122c1SDave Airlie } 480*f64122c1SDave Airlie surf->surface_id = handle; 481*f64122c1SDave Airlie 482*f64122c1SDave Airlie spin_lock(&qdev->surf_id_idr_lock); 483*f64122c1SDave Airlie qdev->last_alloced_surf_id = handle; 484*f64122c1SDave Airlie spin_unlock(&qdev->surf_id_idr_lock); 485*f64122c1SDave Airlie alloc_fail: 486*f64122c1SDave Airlie return 0; 487*f64122c1SDave Airlie } 488*f64122c1SDave Airlie 489*f64122c1SDave Airlie void qxl_surface_id_dealloc(struct qxl_device *qdev, 490*f64122c1SDave Airlie uint32_t surface_id) 491*f64122c1SDave Airlie { 492*f64122c1SDave Airlie spin_lock(&qdev->surf_id_idr_lock); 493*f64122c1SDave Airlie idr_remove(&qdev->surf_id_idr, surface_id); 494*f64122c1SDave Airlie spin_unlock(&qdev->surf_id_idr_lock); 495*f64122c1SDave Airlie } 496*f64122c1SDave Airlie 497*f64122c1SDave Airlie int qxl_hw_surface_alloc(struct qxl_device *qdev, 498*f64122c1SDave Airlie struct qxl_bo *surf, 499*f64122c1SDave Airlie struct ttm_mem_reg *new_mem) 500*f64122c1SDave Airlie { 501*f64122c1SDave Airlie struct qxl_surface_cmd *cmd; 502*f64122c1SDave Airlie struct qxl_release *release; 503*f64122c1SDave Airlie int ret; 504*f64122c1SDave Airlie 505*f64122c1SDave Airlie if (surf->hw_surf_alloc) 506*f64122c1SDave Airlie return 0; 507*f64122c1SDave Airlie 508*f64122c1SDave Airlie ret = qxl_alloc_surface_release_reserved(qdev, QXL_SURFACE_CMD_CREATE, 509*f64122c1SDave Airlie NULL, 510*f64122c1SDave Airlie &release); 511*f64122c1SDave Airlie if (ret) 512*f64122c1SDave Airlie return ret; 513*f64122c1SDave Airlie 514*f64122c1SDave Airlie cmd = (struct qxl_surface_cmd *)qxl_release_map(qdev, release); 515*f64122c1SDave Airlie cmd->type = QXL_SURFACE_CMD_CREATE; 516*f64122c1SDave Airlie cmd->u.surface_create.format = surf->surf.format; 517*f64122c1SDave Airlie cmd->u.surface_create.width = surf->surf.width; 518*f64122c1SDave Airlie cmd->u.surface_create.height = surf->surf.height; 519*f64122c1SDave Airlie cmd->u.surface_create.stride = surf->surf.stride; 520*f64122c1SDave Airlie if (new_mem) { 521*f64122c1SDave Airlie int slot_id = surf->type == QXL_GEM_DOMAIN_VRAM ? qdev->main_mem_slot : qdev->surfaces_mem_slot; 522*f64122c1SDave Airlie struct qxl_memslot *slot = &(qdev->mem_slots[slot_id]); 523*f64122c1SDave Airlie 524*f64122c1SDave Airlie /* TODO - need to hold one of the locks to read tbo.offset */ 525*f64122c1SDave Airlie cmd->u.surface_create.data = slot->high_bits; 526*f64122c1SDave Airlie 527*f64122c1SDave Airlie cmd->u.surface_create.data |= (new_mem->start << PAGE_SHIFT) + surf->tbo.bdev->man[new_mem->mem_type].gpu_offset; 528*f64122c1SDave Airlie } else 529*f64122c1SDave Airlie cmd->u.surface_create.data = qxl_bo_physical_address(qdev, surf, 0); 530*f64122c1SDave Airlie cmd->surface_id = surf->surface_id; 531*f64122c1SDave Airlie qxl_release_unmap(qdev, release, &cmd->release_info); 532*f64122c1SDave Airlie 533*f64122c1SDave Airlie surf->surf_create = release; 534*f64122c1SDave Airlie 535*f64122c1SDave Airlie /* no need to add a release to the fence for this bo, 536*f64122c1SDave Airlie since it is only released when we ask to destroy the surface 537*f64122c1SDave Airlie and it would never signal otherwise */ 538*f64122c1SDave Airlie qxl_fence_releaseable(qdev, release); 539*f64122c1SDave Airlie 540*f64122c1SDave Airlie qxl_push_command_ring_release(qdev, release, QXL_CMD_SURFACE, false); 541*f64122c1SDave Airlie 542*f64122c1SDave Airlie qxl_release_unreserve(qdev, release); 543*f64122c1SDave Airlie 544*f64122c1SDave Airlie surf->hw_surf_alloc = true; 545*f64122c1SDave Airlie spin_lock(&qdev->surf_id_idr_lock); 546*f64122c1SDave Airlie idr_replace(&qdev->surf_id_idr, surf, surf->surface_id); 547*f64122c1SDave Airlie spin_unlock(&qdev->surf_id_idr_lock); 548*f64122c1SDave Airlie return 0; 549*f64122c1SDave Airlie } 550*f64122c1SDave Airlie 551*f64122c1SDave Airlie int qxl_hw_surface_dealloc(struct qxl_device *qdev, 552*f64122c1SDave Airlie struct qxl_bo *surf) 553*f64122c1SDave Airlie { 554*f64122c1SDave Airlie struct qxl_surface_cmd *cmd; 555*f64122c1SDave Airlie struct qxl_release *release; 556*f64122c1SDave Airlie int ret; 557*f64122c1SDave Airlie int id; 558*f64122c1SDave Airlie 559*f64122c1SDave Airlie if (!surf->hw_surf_alloc) 560*f64122c1SDave Airlie return 0; 561*f64122c1SDave Airlie 562*f64122c1SDave Airlie ret = qxl_alloc_surface_release_reserved(qdev, QXL_SURFACE_CMD_DESTROY, 563*f64122c1SDave Airlie surf->surf_create, 564*f64122c1SDave Airlie &release); 565*f64122c1SDave Airlie if (ret) 566*f64122c1SDave Airlie return ret; 567*f64122c1SDave Airlie 568*f64122c1SDave Airlie surf->surf_create = NULL; 569*f64122c1SDave Airlie /* remove the surface from the idr, but not the surface id yet */ 570*f64122c1SDave Airlie spin_lock(&qdev->surf_id_idr_lock); 571*f64122c1SDave Airlie idr_replace(&qdev->surf_id_idr, NULL, surf->surface_id); 572*f64122c1SDave Airlie spin_unlock(&qdev->surf_id_idr_lock); 573*f64122c1SDave Airlie surf->hw_surf_alloc = false; 574*f64122c1SDave Airlie 575*f64122c1SDave Airlie id = surf->surface_id; 576*f64122c1SDave Airlie surf->surface_id = 0; 577*f64122c1SDave Airlie 578*f64122c1SDave Airlie release->surface_release_id = id; 579*f64122c1SDave Airlie cmd = (struct qxl_surface_cmd *)qxl_release_map(qdev, release); 580*f64122c1SDave Airlie cmd->type = QXL_SURFACE_CMD_DESTROY; 581*f64122c1SDave Airlie cmd->surface_id = id; 582*f64122c1SDave Airlie qxl_release_unmap(qdev, release, &cmd->release_info); 583*f64122c1SDave Airlie 584*f64122c1SDave Airlie qxl_fence_releaseable(qdev, release); 585*f64122c1SDave Airlie 586*f64122c1SDave Airlie qxl_push_command_ring_release(qdev, release, QXL_CMD_SURFACE, false); 587*f64122c1SDave Airlie 588*f64122c1SDave Airlie qxl_release_unreserve(qdev, release); 589*f64122c1SDave Airlie 590*f64122c1SDave Airlie 591*f64122c1SDave Airlie return 0; 592*f64122c1SDave Airlie } 593*f64122c1SDave Airlie 594*f64122c1SDave Airlie int qxl_update_surface(struct qxl_device *qdev, struct qxl_bo *surf) 595*f64122c1SDave Airlie { 596*f64122c1SDave Airlie struct qxl_rect rect; 597*f64122c1SDave Airlie int ret; 598*f64122c1SDave Airlie 599*f64122c1SDave Airlie /* if we are evicting, we need to make sure the surface is up 600*f64122c1SDave Airlie to date */ 601*f64122c1SDave Airlie rect.left = 0; 602*f64122c1SDave Airlie rect.right = surf->surf.width; 603*f64122c1SDave Airlie rect.top = 0; 604*f64122c1SDave Airlie rect.bottom = surf->surf.height; 605*f64122c1SDave Airlie retry: 606*f64122c1SDave Airlie ret = qxl_io_update_area(qdev, surf, &rect); 607*f64122c1SDave Airlie if (ret == -ERESTARTSYS) 608*f64122c1SDave Airlie goto retry; 609*f64122c1SDave Airlie return ret; 610*f64122c1SDave Airlie } 611*f64122c1SDave Airlie 612*f64122c1SDave Airlie void qxl_surface_evict_locked(struct qxl_device *qdev, struct qxl_bo *surf, bool do_update_area) 613*f64122c1SDave Airlie { 614*f64122c1SDave Airlie /* no need to update area if we are just freeing the surface normally */ 615*f64122c1SDave Airlie if (do_update_area) 616*f64122c1SDave Airlie qxl_update_surface(qdev, surf); 617*f64122c1SDave Airlie 618*f64122c1SDave Airlie /* nuke the surface id at the hw */ 619*f64122c1SDave Airlie qxl_hw_surface_dealloc(qdev, surf); 620*f64122c1SDave Airlie } 621*f64122c1SDave Airlie 622*f64122c1SDave Airlie void qxl_surface_evict(struct qxl_device *qdev, struct qxl_bo *surf, bool do_update_area) 623*f64122c1SDave Airlie { 624*f64122c1SDave Airlie mutex_lock(&qdev->surf_evict_mutex); 625*f64122c1SDave Airlie qxl_surface_evict_locked(qdev, surf, do_update_area); 626*f64122c1SDave Airlie mutex_unlock(&qdev->surf_evict_mutex); 627*f64122c1SDave Airlie } 628*f64122c1SDave Airlie 629*f64122c1SDave Airlie static int qxl_reap_surf(struct qxl_device *qdev, struct qxl_bo *surf, bool stall) 630*f64122c1SDave Airlie { 631*f64122c1SDave Airlie int ret; 632*f64122c1SDave Airlie 633*f64122c1SDave Airlie ret = qxl_bo_reserve(surf, false); 634*f64122c1SDave Airlie if (ret == -EBUSY) 635*f64122c1SDave Airlie return -EBUSY; 636*f64122c1SDave Airlie 637*f64122c1SDave Airlie if (surf->fence.num_active_releases > 0 && stall == false) { 638*f64122c1SDave Airlie qxl_bo_unreserve(surf); 639*f64122c1SDave Airlie return -EBUSY; 640*f64122c1SDave Airlie } 641*f64122c1SDave Airlie 642*f64122c1SDave Airlie if (stall) 643*f64122c1SDave Airlie mutex_unlock(&qdev->surf_evict_mutex); 644*f64122c1SDave Airlie 645*f64122c1SDave Airlie spin_lock(&surf->tbo.bdev->fence_lock); 646*f64122c1SDave Airlie ret = ttm_bo_wait(&surf->tbo, true, true, !stall); 647*f64122c1SDave Airlie spin_unlock(&surf->tbo.bdev->fence_lock); 648*f64122c1SDave Airlie 649*f64122c1SDave Airlie if (stall) 650*f64122c1SDave Airlie mutex_lock(&qdev->surf_evict_mutex); 651*f64122c1SDave Airlie if (ret == -EBUSY) { 652*f64122c1SDave Airlie qxl_bo_unreserve(surf); 653*f64122c1SDave Airlie return -EBUSY; 654*f64122c1SDave Airlie } 655*f64122c1SDave Airlie 656*f64122c1SDave Airlie qxl_surface_evict_locked(qdev, surf, true); 657*f64122c1SDave Airlie qxl_bo_unreserve(surf); 658*f64122c1SDave Airlie return 0; 659*f64122c1SDave Airlie } 660*f64122c1SDave Airlie 661*f64122c1SDave Airlie static int qxl_reap_surface_id(struct qxl_device *qdev, int max_to_reap) 662*f64122c1SDave Airlie { 663*f64122c1SDave Airlie int num_reaped = 0; 664*f64122c1SDave Airlie int i, ret; 665*f64122c1SDave Airlie bool stall = false; 666*f64122c1SDave Airlie int start = 0; 667*f64122c1SDave Airlie 668*f64122c1SDave Airlie mutex_lock(&qdev->surf_evict_mutex); 669*f64122c1SDave Airlie again: 670*f64122c1SDave Airlie 671*f64122c1SDave Airlie spin_lock(&qdev->surf_id_idr_lock); 672*f64122c1SDave Airlie start = qdev->last_alloced_surf_id + 1; 673*f64122c1SDave Airlie spin_unlock(&qdev->surf_id_idr_lock); 674*f64122c1SDave Airlie 675*f64122c1SDave Airlie for (i = start; i < start + qdev->rom->n_surfaces; i++) { 676*f64122c1SDave Airlie void *objptr; 677*f64122c1SDave Airlie int surfid = i % qdev->rom->n_surfaces; 678*f64122c1SDave Airlie 679*f64122c1SDave Airlie /* this avoids the case where the objects is in the 680*f64122c1SDave Airlie idr but has been evicted half way - its makes 681*f64122c1SDave Airlie the idr lookup atomic with the eviction */ 682*f64122c1SDave Airlie spin_lock(&qdev->surf_id_idr_lock); 683*f64122c1SDave Airlie objptr = idr_find(&qdev->surf_id_idr, surfid); 684*f64122c1SDave Airlie spin_unlock(&qdev->surf_id_idr_lock); 685*f64122c1SDave Airlie 686*f64122c1SDave Airlie if (!objptr) 687*f64122c1SDave Airlie continue; 688*f64122c1SDave Airlie 689*f64122c1SDave Airlie ret = qxl_reap_surf(qdev, objptr, stall); 690*f64122c1SDave Airlie if (ret == 0) 691*f64122c1SDave Airlie num_reaped++; 692*f64122c1SDave Airlie if (num_reaped >= max_to_reap) 693*f64122c1SDave Airlie break; 694*f64122c1SDave Airlie } 695*f64122c1SDave Airlie if (num_reaped == 0 && stall == false) { 696*f64122c1SDave Airlie stall = true; 697*f64122c1SDave Airlie goto again; 698*f64122c1SDave Airlie } 699*f64122c1SDave Airlie 700*f64122c1SDave Airlie mutex_unlock(&qdev->surf_evict_mutex); 701*f64122c1SDave Airlie if (num_reaped) { 702*f64122c1SDave Airlie usleep_range(500, 1000); 703*f64122c1SDave Airlie qxl_queue_garbage_collect(qdev, true); 704*f64122c1SDave Airlie } 705*f64122c1SDave Airlie 706*f64122c1SDave Airlie return 0; 707*f64122c1SDave Airlie } 708