/* r128_drv.c -- ATI Rage 128 driver -*- linux-c -*- * Created: Wed Apr 5 19:24:19 2000 by kevin@precisioninsight.com * * Copyright 2000 Precision Insight, Inc., Cedar Park, Texas. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. * All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice (including the next * paragraph) shall be included in all copies or substantial portions of the * Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. * * Authors: Kevin E. Martin * */ #define __NO_VERSION__ #include "drmP.h" #include "r128_drv.h" #include /* For task queue support */ #include #define DO_REMAP(_m) (_m)->handle = drm_ioremap((_m)->offset, (_m)->size) #define DO_REMAPFREE(_m) \ do { \ if ((_m)->handle && (_m)->size) \ drm_ioremapfree((_m)->handle, (_m)->size); \ } while (0) #define DO_FIND_MAP(_m, _o) \ do { \ int _i; \ for (_i = 0; _i < dev->map_count; _i++) { \ if (dev->maplist[_i]->offset == _o) { \ _m = dev->maplist[_i]; \ break; \ } \ } \ } while (0) #define R128_MAX_VBUF_AGE 0x10000000 #define R128_VB_AGE_REG R128_GUI_SCRATCH_REG0 int R128_READ_PLL(drm_device_t *dev, int addr) { drm_r128_private_t *dev_priv = dev->dev_private; R128_WRITE8(R128_CLOCK_CNTL_INDEX, addr & 0x1f); return R128_READ(R128_CLOCK_CNTL_DATA); } #define r128_flush_write_combine() mb() static void r128_status(drm_device_t *dev) { drm_r128_private_t *dev_priv = dev->dev_private; printk("GUI_STAT = 0x%08x\n", (unsigned int)R128_READ(R128_GUI_STAT)); printk("PM4_STAT = 0x%08x\n", (unsigned int)R128_READ(R128_PM4_STAT)); printk("PM4_BUFFER_DL_WPTR = 0x%08x\n", (unsigned int)R128_READ(R128_PM4_BUFFER_DL_WPTR)); printk("PM4_BUFFER_DL_RPTR = 0x%08x\n", (unsigned int)R128_READ(R128_PM4_BUFFER_DL_RPTR)); } static int r128_do_cleanup_cce(drm_device_t *dev) { if (dev->dev_private) { drm_r128_private_t *dev_priv = dev->dev_private; if (!dev_priv->is_pci) { DO_REMAPFREE(dev_priv->agp_ring); DO_REMAPFREE(dev_priv->agp_read_ptr); DO_REMAPFREE(dev_priv->agp_vertbufs); DO_REMAPFREE(dev_priv->agp_indbufs); DO_REMAPFREE(dev_priv->agp_textures); } drm_free(dev->dev_private, sizeof(drm_r128_private_t), DRM_MEM_DRIVER); dev->dev_private = NULL; } return 0; } static int r128_do_init_cce(drm_device_t *dev, drm_r128_init_t *init) { drm_r128_private_t *dev_priv; int i; dev_priv = drm_alloc(sizeof(drm_r128_private_t), DRM_MEM_DRIVER); if (dev_priv == NULL) return -ENOMEM; dev->dev_private = (void *)dev_priv; memset(dev_priv, 0, sizeof(drm_r128_private_t)); dev_priv->is_pci = init->is_pci; dev_priv->usec_timeout = init->usec_timeout; if (dev_priv->usec_timeout < 1 || dev_priv->usec_timeout > R128_MAX_USEC_TIMEOUT) { drm_free(dev_priv, sizeof(*dev_priv), DRM_MEM_DRIVER); dev->dev_private = NULL; return -EINVAL; } dev_priv->cce_mode = init->cce_mode; dev_priv->cce_fifo_size = init->cce_fifo_size; dev_priv->cce_is_bm_mode = ((init->cce_mode == R128_PM4_192BM) || (init->cce_mode == R128_PM4_128BM_64INDBM) || (init->cce_mode == R128_PM4_64BM_128INDBM) || (init->cce_mode == R128_PM4_64BM_64VCBM_64INDBM)); dev_priv->cce_secure = init->cce_secure; if (dev_priv->cce_is_bm_mode && dev_priv->is_pci) { drm_free(dev_priv, sizeof(*dev_priv), DRM_MEM_DRIVER); dev->dev_private = NULL; return -EINVAL; } for (i = 0; i < dev->map_count; i++) { if (dev->maplist[i]->type == _DRM_SHM) { dev_priv->sarea = dev->maplist[i]; break; } } DO_FIND_MAP(dev_priv->fb, init->fb_offset); if (!dev_priv->is_pci) { DO_FIND_MAP(dev_priv->agp_ring, init->agp_ring_offset); DO_FIND_MAP(dev_priv->agp_read_ptr, init->agp_read_ptr_offset); DO_FIND_MAP(dev_priv->agp_vertbufs, init->agp_vertbufs_offset); DO_FIND_MAP(dev_priv->agp_indbufs, init->agp_indbufs_offset); DO_FIND_MAP(dev_priv->agp_textures, init->agp_textures_offset); } DO_FIND_MAP(dev_priv->mmio, init->mmio_offset); dev_priv->sarea_priv = (drm_r128_sarea_t *)((u8 *)dev_priv->sarea->handle + init->sarea_priv_offset); if (!dev_priv->is_pci) { DO_REMAP(dev_priv->agp_ring); DO_REMAP(dev_priv->agp_read_ptr); DO_REMAP(dev_priv->agp_vertbufs); #if 0 DO_REMAP(dev_priv->agp_indirectbufs); DO_REMAP(dev_priv->agp_textures); #endif dev_priv->ring_size = init->ring_size; dev_priv->ring_sizel2qw = drm_order(init->ring_size/8); dev_priv->ring_entries = init->ring_size/sizeof(u32); dev_priv->ring_read_ptr = ((__volatile__ u32 *) dev_priv->agp_read_ptr->handle); dev_priv->ring_start = (u32 *)dev_priv->agp_ring->handle; dev_priv->ring_end = ((u32 *)dev_priv->agp_ring->handle + dev_priv->ring_entries); } dev_priv->submit_age = 0; R128_WRITE(R128_VB_AGE_REG, dev_priv->submit_age); return 0; } int r128_init_cce(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg) { drm_file_t *priv = filp->private_data; drm_device_t *dev = priv->dev; drm_r128_init_t init; if (copy_from_user(&init, (drm_r128_init_t *)arg, sizeof(init))) return -EFAULT; switch (init.func) { case R128_INIT_CCE: return r128_do_init_cce(dev, &init); case R128_CLEANUP_CCE: return r128_do_cleanup_cce(dev); } return -EINVAL; } static void r128_mark_vertbufs_done(drm_device_t *dev) { drm_device_dma_t *dma = dev->dma; int i; for (i = 0; i < dma->buf_count; i++) { drm_buf_t *buf = dma->buflist[i]; drm_r128_buf_priv_t *buf_priv = buf->dev_private; buf_priv->age = 0; } } static int r128_do_pixcache_flush(drm_device_t *dev) { drm_r128_private_t *dev_priv = dev->dev_private; u32 tmp; int i; tmp = R128_READ(R128_PC_NGUI_CTLSTAT) | R128_PC_FLUSH_ALL; R128_WRITE(R128_PC_NGUI_CTLSTAT, tmp); for (i = 0; i < dev_priv->usec_timeout; i++) { if (!(R128_READ(R128_PC_NGUI_CTLSTAT) & R128_PC_BUSY)) return 0; udelay(1); } return -EBUSY; } static int r128_do_wait_for_fifo(drm_device_t *dev, int entries) { drm_r128_private_t *dev_priv = dev->dev_private; int i; for (i = 0; i < dev_priv->usec_timeout; i++) { int slots = R128_READ(R128_GUI_STAT) & R128_GUI_FIFOCNT_MASK; if (slots >= entries) return 0; udelay(1); } return -EBUSY; } static int r128_do_wait_for_idle(drm_device_t *dev) { drm_r128_private_t *dev_priv = dev->dev_private; int i, ret; if (!(ret = r128_do_wait_for_fifo(dev, 64))) return ret; for (i = 0; i < dev_priv->usec_timeout; i++) { if (!(R128_READ(R128_GUI_STAT) & R128_GUI_ACTIVE)) { (void)r128_do_pixcache_flush(dev); return 0; } udelay(1); } return -EBUSY; } int r128_do_engine_reset(drm_device_t *dev) { drm_r128_private_t *dev_priv = dev->dev_private; u32 clock_cntl_index, mclk_cntl, gen_reset_cntl; (void)r128_do_pixcache_flush(dev); clock_cntl_index = R128_READ(R128_CLOCK_CNTL_INDEX); mclk_cntl = R128_READ_PLL(dev, R128_MCLK_CNTL); R128_WRITE_PLL(R128_MCLK_CNTL, mclk_cntl | R128_FORCE_GCP | R128_FORCE_PIPE3D_CP); gen_reset_cntl = R128_READ(R128_GEN_RESET_CNTL); R128_WRITE(R128_GEN_RESET_CNTL, gen_reset_cntl | R128_SOFT_RESET_GUI); (void)R128_READ(R128_GEN_RESET_CNTL); R128_WRITE(R128_GEN_RESET_CNTL, gen_reset_cntl & ~R128_SOFT_RESET_GUI); (void)R128_READ(R128_GEN_RESET_CNTL); R128_WRITE_PLL(R128_MCLK_CNTL, mclk_cntl); R128_WRITE(R128_CLOCK_CNTL_INDEX, clock_cntl_index); R128_WRITE(R128_GEN_RESET_CNTL, gen_reset_cntl); /* For CCE ring buffer only */ if (dev_priv->cce_is_bm_mode) { R128_WRITE(R128_PM4_BUFFER_DL_WPTR, 0); R128_WRITE(R128_PM4_BUFFER_DL_RPTR, 0); *dev_priv->ring_read_ptr = 0; dev_priv->sarea_priv->ring_write = 0; } /* Reset the CCE mode */ (void)r128_do_wait_for_idle(dev); R128_WRITE(R128_PM4_BUFFER_CNTL, dev_priv->cce_mode | dev_priv->ring_sizel2qw); (void)R128_READ(R128_PM4_BUFFER_ADDR); /* as per the sample code */ R128_WRITE(R128_PM4_MICRO_CNTL, R128_PM4_MICRO_FREERUN); r128_mark_vertbufs_done(dev); return 0; } int r128_eng_reset(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg) { drm_file_t *priv = filp->private_data; drm_device_t *dev = priv->dev; if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) || dev->lock.pid != current->pid) { DRM_ERROR("r128_eng_reset called without holding the lock\n"); return -EINVAL; } return r128_do_engine_reset(dev); } static int r128_do_engine_flush(drm_device_t *dev) { drm_r128_private_t *dev_priv = dev->dev_private; u32 tmp; tmp = R128_READ(R128_PM4_BUFFER_DL_WPTR); R128_WRITE(R128_PM4_BUFFER_DL_WPTR, tmp | R128_PM4_BUFFER_DL_DONE); return 0; } int r128_eng_flush(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg) { drm_file_t *priv = filp->private_data; drm_device_t *dev = priv->dev; if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) || dev->lock.pid != current->pid) { DRM_ERROR("r128_eng_flush called without holding the lock\n"); return -EINVAL; } return r128_do_engine_flush(dev); } static int r128_do_cce_wait_for_fifo(drm_device_t *dev, int entries) { drm_r128_private_t *dev_priv = dev->dev_private; int i; for (i = 0; i < dev_priv->usec_timeout; i++) { int slots = R128_READ(R128_PM4_STAT) & R128_PM4_FIFOCNT_MASK; if (slots >= entries) return 0; udelay(1); } return -EBUSY; } int r128_do_cce_wait_for_idle(drm_device_t *dev) { drm_r128_private_t *dev_priv = dev->dev_private; int i; if (dev_priv->cce_is_bm_mode) { for (i = 0; i < dev_priv->usec_timeout; i++) { if (*dev_priv->ring_read_ptr == dev_priv->sarea_priv->ring_write) { int pm4stat = R128_READ(R128_PM4_STAT); if ((pm4stat & R128_PM4_FIFOCNT_MASK) >= dev_priv->cce_fifo_size && !(pm4stat & (R128_PM4_BUSY | R128_PM4_GUI_ACTIVE))) { return r128_do_pixcache_flush(dev); } } udelay(1); } return -EBUSY; } else { int ret = r128_do_cce_wait_for_fifo(dev, dev_priv->cce_fifo_size); if (ret < 0) return ret; for (i = 0; i < dev_priv->usec_timeout; i++) { int pm4stat = R128_READ(R128_PM4_STAT); if (!(pm4stat & (R128_PM4_BUSY | R128_PM4_GUI_ACTIVE))) { return r128_do_pixcache_flush(dev); } udelay(1); } return -EBUSY; } } int r128_cce_idle(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg) { drm_file_t *priv = filp->private_data; drm_device_t *dev = priv->dev; if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) || dev->lock.pid != current->pid) { DRM_ERROR("r128_wait_idle called without holding the lock\n"); return -EINVAL; } return r128_do_cce_wait_for_idle(dev); } static int r128_submit_packets_ring_secure(drm_device_t *dev, u32 *commands, int *count) { drm_r128_private_t *dev_priv = dev->dev_private; int write = dev_priv->sarea_priv->ring_write; int *write_ptr = dev_priv->ring_start + write; int c = *count; u32 tmp = 0; int psize = 0; int writing = 1; int timeout; while (c > 0) { tmp = *commands++; if (!psize) { writing = 1; if ((tmp & R128_CCE_PACKET_MASK) == R128_CCE_PACKET0) { if ((tmp & R128_CCE_PACKET0_REG_MASK) <= (0x1004 >> 2)) { if ((tmp & R128_CCE_PACKET0_REG_MASK) != (R128_PM4_VC_FPU_SETUP >> 2)) { writing = 0; } } psize = ((tmp & R128_CCE_PACKET_COUNT_MASK) >> 16) + 2; } else if ((tmp & R128_CCE_PACKET_MASK) == R128_CCE_PACKET1) { if ((tmp & R128_CCE_PACKET1_REG0_MASK) <= (0x1004 >> 2)) { if ((tmp & R128_CCE_PACKET1_REG0_MASK) != (R128_PM4_VC_FPU_SETUP >> 2)) { writing = 0; } } else if ((tmp & R128_CCE_PACKET1_REG1_MASK) <= (0x1004 << 9)) { if ((tmp & R128_CCE_PACKET1_REG1_MASK) != (R128_PM4_VC_FPU_SETUP << 9)) { writing = 0; } } psize = 3; } else { psize = ((tmp & R128_CCE_PACKET_COUNT_MASK) >> 16) + 2; } } psize--; if (writing) { write++; *write_ptr++ = tmp; } if (write >= dev_priv->ring_entries) { write = 0; write_ptr = dev_priv->ring_start; } timeout = 0; while (write == *dev_priv->ring_read_ptr) { (void)R128_READ(R128_PM4_BUFFER_DL_RPTR); if (timeout++ >= dev_priv->usec_timeout) return -EBUSY; udelay(1); } c--; } if (write < 32) memcpy(dev_priv->ring_end, dev_priv->ring_start, write * sizeof(u32)); /* Make sure WC cache has been flushed */ r128_flush_write_combine(); dev_priv->sarea_priv->ring_write = write; R128_WRITE(R128_PM4_BUFFER_DL_WPTR, write); *count = 0; return 0; } static int r128_submit_packets_pio_secure(drm_device_t *dev, u32 *commands, int *count) { drm_r128_private_t *dev_priv = dev->dev_private; u32 tmp = 0; int psize = 0; int writing = 1; int addr = R128_PM4_FIFO_DATA_EVEN; int ret; while (*count > 0) { tmp = *commands++; if (!psize) { writing = 1; if ((tmp & R128_CCE_PACKET_MASK) == R128_CCE_PACKET0) { if ((tmp & R128_CCE_PACKET0_REG_MASK) <= (0x1004 >> 2)) { if ((tmp & R128_CCE_PACKET0_REG_MASK) != (R128_PM4_VC_FPU_SETUP >> 2)) { writing = 0; } } psize = ((tmp & R128_CCE_PACKET_COUNT_MASK) >> 16) + 2; } else if ((tmp & R128_CCE_PACKET_MASK) == R128_CCE_PACKET1) { if ((tmp & R128_CCE_PACKET1_REG0_MASK) <= (0x1004 >> 2)) { if ((tmp & R128_CCE_PACKET1_REG0_MASK) != (R128_PM4_VC_FPU_SETUP >> 2)) { writing = 0; } } else if ((tmp & R128_CCE_PACKET1_REG1_MASK) <= (0x1004 << 9)) { if ((tmp & R128_CCE_PACKET1_REG1_MASK) != (R128_PM4_VC_FPU_SETUP << 9)) { writing = 0; } } psize = 3; } else { psize = ((tmp & R128_CCE_PACKET_COUNT_MASK) >> 16) + 2; } } psize--; if (writing) { if ((ret = r128_do_cce_wait_for_fifo(dev, 1)) < 0) return ret; R128_WRITE(addr, tmp); addr ^= 0x0004; } *count -= 1; } if (addr == R128_PM4_FIFO_DATA_ODD) { if ((ret = r128_do_cce_wait_for_fifo(dev, 1)) < 0) return ret; R128_WRITE(addr, R128_CCE_PACKET2); } return 0; } static int r128_submit_packets_ring(drm_device_t *dev, u32 *commands, int *count) { drm_r128_private_t *dev_priv = dev->dev_private; int write = dev_priv->sarea_priv->ring_write; int *write_ptr = dev_priv->ring_start + write; int c = *count; int timeout; while (c > 0) { write++; *write_ptr++ = *commands++; if (write >= dev_priv->ring_entries) { write = 0; write_ptr = dev_priv->ring_start; } timeout = 0; while (write == *dev_priv->ring_read_ptr) { (void)R128_READ(R128_PM4_BUFFER_DL_RPTR); if (timeout++ >= dev_priv->usec_timeout) return -EBUSY; udelay(1); } c--; } if (write < 32) memcpy(dev_priv->ring_end, dev_priv->ring_start, write * sizeof(u32)); /* Make sure WC cache has been flushed */ r128_flush_write_combine(); dev_priv->sarea_priv->ring_write = write; R128_WRITE(R128_PM4_BUFFER_DL_WPTR, write); *count = 0; return 0; } static int r128_submit_packets_pio(drm_device_t *dev, u32 *commands, int *count) { drm_r128_private_t *dev_priv = dev->dev_private; int ret; while (*count > 1) { if ((ret = r128_do_cce_wait_for_fifo(dev, 2)) < 0) return ret; R128_WRITE(R128_PM4_FIFO_DATA_EVEN, *commands++); R128_WRITE(R128_PM4_FIFO_DATA_ODD, *commands++); *count -= 2; } if (*count) { if ((ret = r128_do_cce_wait_for_fifo(dev, 2)) < 0) return ret; R128_WRITE(R128_PM4_FIFO_DATA_EVEN, *commands++); R128_WRITE(R128_PM4_FIFO_DATA_ODD, R128_CCE_PACKET2); *count = 0; } return 0; } static int r128_do_submit_packets(drm_device_t *dev, u32 *buffer, int count) { drm_r128_private_t *dev_priv = dev->dev_private; int c = count; int ret; if (dev_priv->cce_is_bm_mode) { int left = 0; if (c >= dev_priv->ring_entries) { c = dev_priv->ring_entries-1; left = count - c; } /* Since this is only used by the kernel we can use the insecure ring buffer submit packet routine */ ret = r128_submit_packets_ring(dev, buffer, &c); c += left; } else { /* Since this is only used by the kernel we can use the insecure PIO submit packet routine */ ret = r128_submit_packets_pio(dev, buffer, &c); } if (ret < 0) return ret; else return c; } int r128_submit_pkt(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg) { drm_file_t *priv = filp->private_data; drm_device_t *dev = priv->dev; drm_r128_private_t *dev_priv = dev->dev_private; drm_r128_packet_t packet; u32 *buffer; int c; int size; int ret = 0; if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) || dev->lock.pid != current->pid) { DRM_ERROR("r128_submit_pkt called without holding the lock\n"); return -EINVAL; } if (copy_from_user(&packet, (drm_r128_packet_t *)arg, sizeof(packet))) return -EFAULT; c = packet.count; size = c * sizeof(*buffer); if (dev_priv->cce_is_bm_mode) { int left = 0; if (c >= dev_priv->ring_entries) { c = dev_priv->ring_entries-1; size = c * sizeof(*buffer); left = packet.count - c; } if ((buffer = kmalloc(size, 0)) == NULL) return -ENOMEM; if (copy_from_user(buffer, packet.buffer, size)) return -EFAULT; if (dev_priv->cce_secure) ret = r128_submit_packets_ring_secure(dev, buffer, &c); else ret = r128_submit_packets_ring(dev, buffer, &c); c += left; } else { if ((buffer = kmalloc(size, 0)) == NULL) return -ENOMEM; if (copy_from_user(buffer, packet.buffer, size)) return -EFAULT; if (dev_priv->cce_secure) ret = r128_submit_packets_pio_secure(dev, buffer, &c); else ret = r128_submit_packets_pio(dev, buffer, &c); } kfree(buffer); packet.count = c; if (copy_to_user((drm_r128_packet_t *)arg, &packet, sizeof(packet))) return -EFAULT; if (ret) return ret; else if (c > 0) return -EAGAIN; return 0; } static int r128_send_vertbufs(drm_device_t *dev, drm_r128_vertex_t *v) { drm_device_dma_t *dma = dev->dma; drm_r128_private_t *dev_priv = dev->dev_private; drm_r128_buf_priv_t *buf_priv; drm_buf_t *buf; int i, ret; u32 cce[2]; /* Make sure we have valid data */ for (i = 0; i < v->send_count; i++) { int idx = v->send_indices[i]; if (idx < 0 || idx >= dma->buf_count) { DRM_ERROR("Index %d (of %d max)\n", idx, dma->buf_count - 1); return -EINVAL; } buf = dma->buflist[idx]; if (buf->pid != current->pid) { DRM_ERROR("Process %d using buffer owned by %d\n", current->pid, buf->pid); return -EINVAL; } if (buf->pending) { DRM_ERROR("Sending pending buffer:" " buffer %d, offset %d\n", v->send_indices[i], i); return -EINVAL; } } /* Wait for idle, if we've wrapped to make sure that all pending buffers have been processed */ if (dev_priv->submit_age == R128_MAX_VBUF_AGE) { if ((ret = r128_do_cce_wait_for_idle(dev)) < 0) return ret; dev_priv->submit_age = 0; r128_mark_vertbufs_done(dev); } /* Make sure WC cache has been flushed (if in PIO mode) */ if (!dev_priv->cce_is_bm_mode) r128_flush_write_combine(); /* FIXME: Add support for sending vertex buffer to the CCE here instead of in client code. The v->prim holds the primitive type that should be drawn. Loop over the list buffers in send_indices[] and submit a packet for each VB. This will require us to loop over the clip rects here as well, which implies that we extend the kernel driver to allow cliprects to be stored here. Note that the cliprects could possibly come from the X server instead of the client, but this will require additional changes to the DRI to allow for this optimization. */ /* Submit a CCE packet that writes submit_age to R128_VB_AGE_REG */ cce[0] = R128CCE0(R128_CCE_PACKET0, R128_VB_AGE_REG, 0); cce[1] = dev_priv->submit_age; if ((ret = r128_do_submit_packets(dev, cce, 2)) < 0) { /* Until we add support for sending VBs to the CCE in this routine, we can recover from this error. After we add that support, we won't be able to easily recover, so we will probably have to implement another mechanism for handling timeouts from packets submitted directly by the kernel. */ return ret; } /* Now that the submit packet request has succeeded, we can mark the buffers as pending */ for (i = 0; i < v->send_count; i++) { buf = dma->buflist[v->send_indices[i]]; buf->pending = 1; buf_priv = buf->dev_private; buf_priv->age = dev_priv->submit_age; } dev_priv->submit_age++; return 0; } static drm_buf_t *r128_freelist_get(drm_device_t *dev) { drm_device_dma_t *dma = dev->dma; drm_r128_private_t *dev_priv = dev->dev_private; drm_r128_buf_priv_t *buf_priv; drm_buf_t *buf; int i, t; /* FIXME: Optimize -- use freelist code */ for (i = 0; i < dma->buf_count; i++) { buf = dma->buflist[i]; buf_priv = buf->dev_private; if (buf->pid == 0) return buf; } for (t = 0; t < dev_priv->usec_timeout; t++) { u32 done_age = R128_READ(R128_VB_AGE_REG); for (i = 0; i < dma->buf_count; i++) { buf = dma->buflist[i]; buf_priv = buf->dev_private; if (buf->pending && buf_priv->age <= done_age) { /* The buffer has been processed, so it can now be used */ buf->pending = 0; return buf; } } udelay(1); } r128_status(dev); return NULL; } static int r128_get_vertbufs(drm_device_t *dev, drm_r128_vertex_t *v) { drm_buf_t *buf; int i; for (i = v->granted_count; i < v->request_count; i++) { buf = r128_freelist_get(dev); if (!buf) break; buf->pid = current->pid; if (copy_to_user(&v->request_indices[i], &buf->idx, sizeof(buf->idx)) || copy_to_user(&v->request_sizes[i], &buf->total, sizeof(buf->total))) return -EFAULT; ++v->granted_count; } return 0; } int r128_vertex_buf(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg) { drm_file_t *priv = filp->private_data; drm_device_t *dev = priv->dev; drm_r128_private_t *dev_priv = dev->dev_private; drm_device_dma_t *dma = dev->dma; int retcode = 0; drm_r128_vertex_t v; if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) || dev->lock.pid != current->pid) { DRM_ERROR("r128_vertex_buf called without holding the lock\n"); return -EINVAL; } if (!dev_priv || dev_priv->is_pci) { DRM_ERROR("r128_vertex_buf called with a PCI card\n"); return -EINVAL; } if (copy_from_user(&v, (drm_r128_vertex_t *)arg, sizeof(v))) return -EFAULT; DRM_DEBUG("%d: %d send, %d req\n", current->pid, v.send_count, v.request_count); if (v.send_count < 0 || v.send_count > dma->buf_count) { DRM_ERROR("Process %d trying to send %d buffers (of %d max)\n", current->pid, v.send_count, dma->buf_count); return -EINVAL; } if (v.request_count < 0 || v.request_count > dma->buf_count) { DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n", current->pid, v.request_count, dma->buf_count); return -EINVAL; } if (v.send_count) { retcode = r128_send_vertbufs(dev, &v); } v.granted_count = 0; if (!retcode && v.request_count) { retcode = r128_get_vertbufs(dev, &v); } DRM_DEBUG("%d returning, granted = %d\n", current->pid, v.granted_count); if (copy_to_user((drm_r128_vertex_t *)arg, &v, sizeof(v))) return -EFAULT; return retcode; } a> 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
/**************************************************************************
 *
 * This kernel module is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 **************************************************************************/
/*
 * This code provides access to unexported mm kernel features. It is necessary
 * to use the new DRM memory manager code with kernels that don't support it
 * directly.
 *
 * Authors: Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
 *          Linux kernel mm subsystem authors.
 *          (Most code taken from there).
 */

#include "drmP.h"

#if defined(CONFIG_X86) && (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15))

/*
 * These have bad performance in the AGP module for the indicated kernel versions.
 */

int drm_map_page_into_agp(struct page *page)
{
        int i;
        i = change_page_attr(page, 1, PAGE_KERNEL_NOCACHE);
        /* Caller's responsibility to call global_flush_tlb() for
         * performance reasons */
        return i;
}

int drm_unmap_page_from_agp(struct page *page)
{
        int i;
        i = change_page_attr(page, 1, PAGE_KERNEL);
        /* Caller's responsibility to call global_flush_tlb() for
         * performance reasons */
        return i;
}
#endif


#if  (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))

/*
 * The protection map was exported in 2.6.19
 */

pgprot_t vm_get_page_prot(unsigned long vm_flags)
{
#ifdef MODULE
	static pgprot_t drm_protection_map[16] = {
		__P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
		__S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
	};

	return drm_protection_map[vm_flags & 0x0F];
#else
	extern pgprot_t protection_map[];
	return protection_map[vm_flags & 0x0F];
#endif
};
#endif


#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15))

/*
 * vm code for kernels below 2.6.15 in which version a major vm write
 * occured. This implement a simple straightforward
 * version similar to what's going to be
 * in kernel 2.6.19+
 * Kernels below 2.6.15 use nopage whereas 2.6.19 and upwards use
 * nopfn.
 */

static struct {
	spinlock_t lock;
	struct page *dummy_page;
	atomic_t present;
} drm_np_retry =
{SPIN_LOCK_UNLOCKED, NOPAGE_OOM, ATOMIC_INIT(0)};


static struct page *drm_bo_vm_fault(struct vm_area_struct *vma,
				    struct fault_data *data);


struct page * get_nopage_retry(void)
{
	if (atomic_read(&drm_np_retry.present) == 0) {
		struct page *page = alloc_page(GFP_KERNEL);
		if (!page)
			return NOPAGE_OOM;
		spin_lock(&drm_np_retry.lock);
		drm_np_retry.dummy_page = page;
		atomic_set(&drm_np_retry.present,1);
		spin_unlock(&drm_np_retry.lock);
	}
	get_page(drm_np_retry.dummy_page);
	return drm_np_retry.dummy_page;
}

void free_nopage_retry(void)
{
	if (atomic_read(&drm_np_retry.present) == 1) {
		spin_lock(&drm_np_retry.lock);
		__free_page(drm_np_retry.dummy_page);
		drm_np_retry.dummy_page = NULL;
		atomic_set(&drm_np_retry.present, 0);
		spin_unlock(&drm_np_retry.lock);
	}
}

struct page *drm_bo_vm_nopage(struct vm_area_struct *vma,
			       unsigned long address,
			       int *type)
{
	struct fault_data data;

	if (type)
		*type = VM_FAULT_MINOR;

	data.address = address;
	data.vma = vma;
	drm_bo_vm_fault(vma, &data);
	switch (data.type) {
	case VM_FAULT_OOM:
		return NOPAGE_OOM;
	case VM_FAULT_SIGBUS:
		return NOPAGE_SIGBUS;
	default:
		break;
	}

	return NOPAGE_REFAULT;
}

#endif

#if !defined(DRM_FULL_MM_COMPAT) && \
  ((LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)) || \
   (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)))

static int drm_pte_is_clear(struct vm_area_struct *vma,
			    unsigned long addr)
{
	struct mm_struct *mm = vma->vm_mm;
	int ret = 1;
	pte_t *pte;
	pmd_t *pmd;
	pud_t *pud;
	pgd_t *pgd;

	spin_lock(&mm->page_table_lock);
	pgd = pgd_offset(mm, addr);
	if (pgd_none(*pgd))
		goto unlock;
	pud = pud_offset(pgd, addr);
        if (pud_none(*pud))
		goto unlock;
	pmd = pmd_offset(pud, addr);
	if (pmd_none(*pmd))
		goto unlock;
	pte = pte_offset_map(pmd, addr);
	if (!pte)
		goto unlock;
	ret = pte_none(*pte);
	pte_unmap(pte);
 unlock:
	spin_unlock(&mm->page_table_lock);
	return ret;
}

static int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
		  unsigned long pfn)
{
	int ret;
	if (!drm_pte_is_clear(vma, addr))
		return -EBUSY;

	ret = io_remap_pfn_range(vma, addr, pfn, PAGE_SIZE, vma->vm_page_prot);
	return ret;
}


static struct page *drm_bo_vm_fault(struct vm_area_struct *vma,
				    struct fault_data *data)
{
	unsigned long address = data->address;
	struct drm_buffer_object *bo = (struct drm_buffer_object *) vma->vm_private_data;
	unsigned long page_offset;
	struct page *page = NULL;
	struct drm_ttm *ttm;
	struct drm_device *dev;
	unsigned long pfn;
	int err;
	unsigned long bus_base;
	unsigned long bus_offset;
	unsigned long bus_size;

	dev = bo->dev;
	while(drm_bo_read_lock(&dev->bm.bm_lock));

	mutex_lock(&bo->mutex);

	err = drm_bo_wait(bo, 0, 1, 0);
	if (err) {
		data->type = (err == -EAGAIN) ?
			VM_FAULT_MINOR : VM_FAULT_SIGBUS;
		goto out_unlock;
	}


	/*
	 * If buffer happens to be in a non-mappable location,
	 * move it to a mappable.
	 */

	if (!(bo->mem.flags & DRM_BO_FLAG_MAPPABLE)) {
		unsigned long _end = jiffies + 3*DRM_HZ;
		uint32_t new_mask = bo->mem.proposed_flags |
			DRM_BO_FLAG_MAPPABLE |
			DRM_BO_FLAG_FORCE_MAPPABLE;

		do {
			err = drm_bo_move_buffer(bo, new_mask, 0, 0);
		} while((err == -EAGAIN) && !time_after_eq(jiffies, _end));

		if (err) {
			DRM_ERROR("Timeout moving buffer to mappable location.\n");
			data->type = VM_FAULT_SIGBUS;
			goto out_unlock;
		}
	}

	if (address > vma->vm_end) {
		data->type = VM_FAULT_SIGBUS;
		goto out_unlock;
	}

	dev = bo->dev;
	err = drm_bo_pci_offset(dev, &bo->mem, &bus_base, &bus_offset,
				&bus_size);

	if (err) {
		data->type = VM_FAULT_SIGBUS;
		goto out_unlock;
	}

	page_offset = (address - vma->vm_start) >> PAGE_SHIFT;

	if (bus_size) {
		struct drm_mem_type_manager *man = &dev->bm.man[bo->mem.mem_type];

		pfn = ((bus_base + bus_offset) >> PAGE_SHIFT) + page_offset;
		vma->vm_page_prot = drm_io_prot(man->drm_bus_maptype, vma);
	} else {
		ttm = bo->ttm;

		drm_ttm_fixup_caching(ttm);
		page = drm_ttm_get_page(ttm, page_offset);
		if (!page) {
			data->type = VM_FAULT_OOM;
			goto out_unlock;
		}
		pfn = page_to_pfn(page);
		vma->vm_page_prot = (bo->mem.flags & DRM_BO_FLAG_CACHED) ?
			vm_get_page_prot(vma->vm_flags) :
			drm_io_prot(_DRM_TTM, vma);
	}

	err = vm_insert_pfn(vma, address, pfn);

	if (!err || err == -EBUSY)
		data->type = VM_FAULT_MINOR;
	else
		data->type = VM_FAULT_OOM;
out_unlock:
	mutex_unlock(&bo->mutex);
	drm_bo_read_unlock(&dev->bm.bm_lock);
	return NULL;
}

#endif

#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)) && \
  !defined(DRM_FULL_MM_COMPAT)

/**
 */

unsigned long drm_bo_vm_nopfn(struct vm_area_struct * vma,
			   unsigned long address)
{
	struct fault_data data;
	data.address = address;

	(void) drm_bo_vm_fault(vma, &data);
	if (data.type == VM_FAULT_OOM)
		return NOPFN_OOM;
	else if (data.type == VM_FAULT_SIGBUS)
		return NOPFN_SIGBUS;

	/*
	 * pfn already set.
	 */

	return 0;
}
#endif


#ifdef DRM_ODD_MM_COMPAT

/*
 * VM compatibility code for 2.6.15-2.6.18. This code implements a complicated
 * workaround for a single BUG statement in do_no_page in these versions. The
 * tricky thing is that we need to take the mmap_sem in exclusive mode for _all_
 * vmas mapping the ttm, before dev->struct_mutex is taken. The way we do this is to
 * check first take the dev->struct_mutex, and then trylock all mmap_sems. If this
 * fails for a single mmap_sem, we have to release all sems and the dev->struct_mutex,
 * release the cpu and retry. We also need to keep track of all vmas mapping the ttm.
 * phew.
 */

typedef struct p_mm_entry {
	struct list_head head;
	struct mm_struct *mm;
	atomic_t refcount;
        int locked;
} p_mm_entry_t;

typedef struct vma_entry {
	struct list_head head;
	struct vm_area_struct *vma;
} vma_entry_t;


struct page *drm_bo_vm_nopage(struct vm_area_struct *vma,
			       unsigned long address,
			       int *type)
{
	struct drm_buffer_object *bo = (struct drm_buffer_object *) vma->vm_private_data;
	unsigned long page_offset;
	struct page *page;
	struct drm_ttm *ttm;
	struct drm_device *dev;

	mutex_lock(&bo->mutex);

	if (type)
		*type = VM_FAULT_MINOR;

	if (address > vma->vm_end) {
		page = NOPAGE_SIGBUS;
		goto out_unlock;
	}

	dev = bo->dev;

	if (drm_mem_reg_is_pci(dev, &bo->mem)) {
		DRM_ERROR("Invalid compat nopage.\n");
		page = NOPAGE_SIGBUS;
		goto out_unlock;
	}

	ttm = bo->ttm;
	drm_ttm_fixup_caching(ttm);
	page_offset = (address - vma->vm_start) >> PAGE_SHIFT;
	page = drm_ttm_get_page(ttm, page_offset);
	if (!page) {
		page = NOPAGE_OOM;
		goto out_unlock;
	}

	get_page(page);
out_unlock:
	mutex_unlock(&bo->mutex);
	return page;
}




int drm_bo_map_bound(struct vm_area_struct *vma)
{
	struct drm_buffer_object *bo = (struct drm_buffer_object *)vma->vm_private_data;
	int ret = 0;
	unsigned long bus_base;
	unsigned long bus_offset;
	unsigned long bus_size;

	ret = drm_bo_pci_offset(bo->dev, &bo->mem, &bus_base,
				&bus_offset, &bus_size);
	BUG_ON(ret);

	if (bus_size) {
		struct drm_mem_type_manager *man = &bo->dev->bm.man[bo->mem.mem_type];
		unsigned long pfn = (bus_base + bus_offset) >> PAGE_SHIFT;
		pgprot_t pgprot = drm_io_prot(man->drm_bus_maptype, vma);
		ret = io_remap_pfn_range(vma, vma->vm_start, pfn,
					 vma->vm_end - vma->vm_start,
					 pgprot);
	}

	return ret;
}


int drm_bo_add_vma(struct drm_buffer_object * bo, struct vm_area_struct *vma)
{
	p_mm_entry_t *entry, *n_entry;
	vma_entry_t *v_entry;
	struct mm_struct *mm = vma->vm_mm;

	v_entry = drm_ctl_alloc(sizeof(*v_entry), DRM_MEM_BUFOBJ);
	if (!v_entry) {
		DRM_ERROR("Allocation of vma pointer entry failed\n");
		return -ENOMEM;
	}
	v_entry->vma = vma;

	list_add_tail(&v_entry->head, &bo->vma_list);

	list_for_each_entry(entry, &bo->p_mm_list, head) {
		if (mm == entry->mm) {
			atomic_inc(&entry->refcount);
			return 0;
		} else if ((unsigned long)mm < (unsigned long)entry->mm) ;
	}

	n_entry = drm_ctl_alloc(sizeof(*n_entry), DRM_MEM_BUFOBJ);
	if (!n_entry) {
		DRM_ERROR("Allocation of process mm pointer entry failed\n");
		return -ENOMEM;
	}
	INIT_LIST_HEAD(&n_entry->head);
	n_entry->mm = mm;
	n_entry->locked = 0;
	atomic_set(&n_entry->refcount, 0);
	list_add_tail(&n_entry->head, &entry->head);

	return 0;
}

void drm_bo_delete_vma(struct drm_buffer_object * bo, struct vm_area_struct *vma)
{
	p_mm_entry_t *entry, *n;
	vma_entry_t *v_entry, *v_n;
	int found = 0;
	struct mm_struct *mm = vma->vm_mm;

	list_for_each_entry_safe(v_entry, v_n, &bo->vma_list, head) {
		if (v_entry->vma == vma) {
			found = 1;
			list_del(&v_entry->head);
			drm_ctl_free(v_entry, sizeof(*v_entry), DRM_MEM_BUFOBJ);
			break;
		}
	}
	BUG_ON(!found);

	list_for_each_entry_safe(entry, n, &bo->p_mm_list, head) {
		if (mm == entry->mm) {
			if (atomic_add_negative(-1, &entry->refcount)) {
				list_del(&entry->head);
				BUG_ON(entry->locked);
				drm_ctl_free(entry, sizeof(*entry), DRM_MEM_BUFOBJ);
			}
			return;
		}
	}
	BUG_ON(1);
}



int drm_bo_lock_kmm(struct drm_buffer_object * bo)
{
	p_mm_entry_t *entry;
	int lock_ok = 1;

	list_for_each_entry(entry, &bo->p_mm_list, head) {
		BUG_ON(entry->locked);
		if (!down_write_trylock(&entry->mm->mmap_sem)) {
			lock_ok = 0;
			break;
		}
		entry->locked = 1;
	}

	if (lock_ok)
		return 0;

	list_for_each_entry(entry, &bo->p_mm_list, head) {
		if (!entry->locked)
			break;
		up_write(&entry->mm->mmap_sem);
		entry->locked = 0;
	}

	/*
	 * Possible deadlock. Try again. Our callers should handle this
	 * and restart.
	 */

	return -EAGAIN;
}

void drm_bo_unlock_kmm(struct drm_buffer_object * bo)
{
	p_mm_entry_t *entry;

	list_for_each_entry(entry, &bo->p_mm_list, head) {
		BUG_ON(!entry->locked);
		up_write(&entry->mm->mmap_sem);
		entry->locked = 0;
	}
}

int drm_bo_remap_bound(struct drm_buffer_object *bo)
{
	vma_entry_t *v_entry;
	int ret = 0;

	if (drm_mem_reg_is_pci(bo->dev, &bo->mem)) {
		list_for_each_entry(v_entry, &bo->vma_list, head) {
			ret = drm_bo_map_bound(v_entry->vma);
			if (ret)
				break;
		}
	}

	return ret;
}

void drm_bo_finish_unmap(struct drm_buffer_object *bo)
{
	vma_entry_t *v_entry;

	list_for_each_entry(v_entry, &bo->vma_list, head) {
		v_entry->vma->vm_flags &= ~VM_PFNMAP;
	}
}

#endif

#ifdef DRM_IDR_COMPAT_FN
/* only called when idp->lock is held */
static void __free_layer(struct idr *idp, struct idr_layer *p)
{
	p->ary[0] = idp->id_free;
	idp->id_free = p;
	idp->id_free_cnt++;
}

static void free_layer(struct idr *idp, struct idr_layer *p)
{
	unsigned long flags;

	/*
	 * Depends on the return element being zeroed.
	 */
	spin_lock_irqsave(&idp->lock, flags);
	__free_layer(idp, p);
	spin_unlock_irqrestore(&idp->lock, flags);
}

/**
 * idr_for_each - iterate through all stored pointers
 * @idp: idr handle
 * @fn: function to be called for each pointer
 * @data: data passed back to callback function
 *
 * Iterate over the pointers registered with the given idr.  The
 * callback function will be called for each pointer currently
 * registered, passing the id, the pointer and the data pointer passed
 * to this function.  It is not safe to modify the idr tree while in
 * the callback, so functions such as idr_get_new and idr_remove are
 * not allowed.
 *
 * We check the return of @fn each time. If it returns anything other
 * than 0, we break out and return that value.
 *
* The caller must serialize idr_find() vs idr_get_new() and idr_remove().
 */
int idr_for_each(struct idr *idp,
		 int (*fn)(int id, void *p, void *data), void *data)
{
	int n, id, max, error = 0;
	struct idr_layer *p;
	struct idr_layer *pa[MAX_LEVEL];
	struct idr_layer **paa = &pa[0];

	n = idp->layers * IDR_BITS;
	p = idp->top;
	max = 1 << n;

	id = 0;
	while (id < max) {
		while (n > 0 && p) {
			n -= IDR_BITS;
			*paa++ = p;
			p = p->ary[(id >> n) & IDR_MASK];
		}

		if (p) {
			error = fn(id, (void *)p, data);
			if (error)
				break;
		}

		id += 1 << n;
		while (n < fls(id)) {
			n += IDR_BITS;
			p = *--paa;
		}
	}

	return error;
}
EXPORT_SYMBOL(idr_for_each);

/**
 * idr_remove_all - remove all ids from the given idr tree
 * @idp: idr handle
 *
 * idr_destroy() only frees up unused, cached idp_layers, but this
 * function will remove all id mappings and leave all idp_layers
 * unused.
 *
 * A typical clean-up sequence for objects stored in an idr tree, will
 * use idr_for_each() to free all objects, if necessay, then
 * idr_remove_all() to remove all ids, and idr_destroy() to free
 * up the cached idr_layers.
 */
void idr_remove_all(struct idr *idp)
{
       int n, id, max, error = 0;
       struct idr_layer *p;
       struct idr_layer *pa[MAX_LEVEL];
       struct idr_layer **paa = &pa[0];

       n = idp->layers * IDR_BITS;
       p = idp->top;
       max = 1 << n;

       id = 0;
       while (id < max && !error) {
               while (n > IDR_BITS && p) {
                       n -= IDR_BITS;
                       *paa++ = p;
                       p = p->ary[(id >> n) & IDR_MASK];
               }

               id += 1 << n;
               while (n < fls(id)) {
                       if (p) {
                               memset(p, 0, sizeof *p);
                               free_layer(idp, p);
                       }
                       n += IDR_BITS;
                       p = *--paa;
               }
       }
       idp->top = NULL;
       idp->layers = 0;
}
EXPORT_SYMBOL(idr_remove_all);

#endif /* DRM_IDR_COMPAT_FN */



#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
/**
 * idr_replace - replace pointer for given id
 * @idp: idr handle
 * @ptr: pointer you want associated with the id
 * @id: lookup key
 *
 * Replace the pointer registered with an id and return the old value.
 * A -ENOENT return indicates that @id was not found.
 * A -EINVAL return indicates that @id was not within valid constraints.
 *
 * The caller must serialize vs idr_find(), idr_get_new(), and idr_remove().
 */
void *idr_replace(struct idr *idp, void *ptr, int id)
{
	int n;
	struct idr_layer *p, *old_p;

	n = idp->layers * IDR_BITS;
	p = idp->top;

	id &= MAX_ID_MASK;

	if (id >= (1 << n))
		return ERR_PTR(-EINVAL);

	n -= IDR_BITS;
	while ((n > 0) && p) {
		p = p->ary[(id >> n) & IDR_MASK];
		n -= IDR_BITS;
	}

	n = id & IDR_MASK;
	if (unlikely(p == NULL || !test_bit(n, &p->bitmap)))
		return ERR_PTR(-ENOENT);

	old_p = p->ary[n];
	p->ary[n] = ptr;

	return (void *)old_p;
}
EXPORT_SYMBOL(idr_replace);
#endif

#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
static __inline__ unsigned long __round_jiffies(unsigned long j, int cpu)
{
	int rem;
	unsigned long original = j;

	j += cpu * 3;

	rem = j % HZ;

	if (rem < HZ/4) /* round down */
		j = j - rem;
	else /* round up */
		j = j - rem + HZ;

	/* now that we have rounded, subtract the extra skew again */
	j -= cpu * 3;

	if (j <= jiffies) /* rounding ate our timeout entirely; */
		return original;
	return j;
}

static __inline__ unsigned long __round_jiffies_relative(unsigned long j, int cpu)
{
	return  __round_jiffies(j + jiffies, cpu) - jiffies;
}

unsigned long round_jiffies_relative(unsigned long j)
{
	return __round_jiffies_relative(j, raw_smp_processor_id());
}
EXPORT_SYMBOL(round_jiffies_relative);
#endif

#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
struct pci_dev * pci_get_bus_and_slot(unsigned int bus, unsigned int devfn)
{
    struct pci_dev *dev = NULL;

    while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
        if (pci_domain_nr(dev->bus) == 0 &&
           (dev->bus->number == bus && dev->devfn == devfn))
            return dev;
   }
   return NULL;
}
EXPORT_SYMBOL(pci_get_bus_and_slot);
#endif