Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* $Id: ffb_drv.c,v 1.16 2001/10/18 16:00:24 davem Exp $ |
| 2 | * ffb_drv.c: Creator/Creator3D direct rendering driver. |
| 3 | * |
| 4 | * Copyright (C) 2000 David S. Miller (davem@redhat.com) |
| 5 | */ |
| 6 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include "ffb.h" |
| 8 | #include "drmP.h" |
| 9 | |
| 10 | #include "ffb_drv.h" |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/smp_lock.h> |
| 13 | #include <asm/shmparam.h> |
| 14 | #include <asm/oplib.h> |
| 15 | #include <asm/upa.h> |
| 16 | |
| 17 | #define DRIVER_AUTHOR "David S. Miller" |
| 18 | |
| 19 | #define DRIVER_NAME "ffb" |
| 20 | #define DRIVER_DESC "Creator/Creator3D" |
| 21 | #define DRIVER_DATE "20000517" |
| 22 | |
| 23 | #define DRIVER_MAJOR 0 |
| 24 | #define DRIVER_MINOR 0 |
| 25 | #define DRIVER_PATCHLEVEL 1 |
| 26 | |
| 27 | typedef struct _ffb_position_t { |
| 28 | int node; |
| 29 | int root; |
| 30 | } ffb_position_t; |
| 31 | |
| 32 | static ffb_position_t *ffb_position; |
| 33 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 34 | static void get_ffb_type(ffb_dev_priv_t * ffb_priv, int instance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | { |
| 36 | volatile unsigned char *strap_bits; |
| 37 | unsigned char val; |
| 38 | |
| 39 | strap_bits = (volatile unsigned char *) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 40 | (ffb_priv->card_phys_base + 0x00200000UL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | /* Don't ask, you have to read the value twice for whatever |
| 43 | * reason to get correct contents. |
| 44 | */ |
| 45 | val = upa_readb(strap_bits); |
| 46 | val = upa_readb(strap_bits); |
| 47 | switch (val & 0x78) { |
| 48 | case (0x0 << 5) | (0x0 << 3): |
| 49 | ffb_priv->ffb_type = ffb1_prototype; |
| 50 | printk("ffb%d: Detected FFB1 pre-FCS prototype\n", instance); |
| 51 | break; |
| 52 | case (0x0 << 5) | (0x1 << 3): |
| 53 | ffb_priv->ffb_type = ffb1_standard; |
| 54 | printk("ffb%d: Detected FFB1\n", instance); |
| 55 | break; |
| 56 | case (0x0 << 5) | (0x3 << 3): |
| 57 | ffb_priv->ffb_type = ffb1_speedsort; |
| 58 | printk("ffb%d: Detected FFB1-SpeedSort\n", instance); |
| 59 | break; |
| 60 | case (0x1 << 5) | (0x0 << 3): |
| 61 | ffb_priv->ffb_type = ffb2_prototype; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 62 | printk("ffb%d: Detected FFB2/vertical pre-FCS prototype\n", |
| 63 | instance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | break; |
| 65 | case (0x1 << 5) | (0x1 << 3): |
| 66 | ffb_priv->ffb_type = ffb2_vertical; |
| 67 | printk("ffb%d: Detected FFB2/vertical\n", instance); |
| 68 | break; |
| 69 | case (0x1 << 5) | (0x2 << 3): |
| 70 | ffb_priv->ffb_type = ffb2_vertical_plus; |
| 71 | printk("ffb%d: Detected FFB2+/vertical\n", instance); |
| 72 | break; |
| 73 | case (0x2 << 5) | (0x0 << 3): |
| 74 | ffb_priv->ffb_type = ffb2_horizontal; |
| 75 | printk("ffb%d: Detected FFB2/horizontal\n", instance); |
| 76 | break; |
| 77 | case (0x2 << 5) | (0x2 << 3): |
| 78 | ffb_priv->ffb_type = ffb2_horizontal; |
| 79 | printk("ffb%d: Detected FFB2+/horizontal\n", instance); |
| 80 | break; |
| 81 | default: |
| 82 | ffb_priv->ffb_type = ffb2_vertical; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 83 | printk("ffb%d: Unknown boardID[%08x], assuming FFB2\n", |
| 84 | instance, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | break; |
| 86 | }; |
| 87 | } |
| 88 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 89 | static void ffb_apply_upa_parent_ranges(int parent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | struct linux_prom64_registers *regs) |
| 91 | { |
| 92 | struct linux_prom64_ranges ranges[PROMREG_MAX]; |
| 93 | char name[128]; |
| 94 | int len, i; |
| 95 | |
| 96 | prom_getproperty(parent, "name", name, sizeof(name)); |
| 97 | if (strcmp(name, "upa") != 0) |
| 98 | return; |
| 99 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 100 | len = |
| 101 | prom_getproperty(parent, "ranges", (void *)ranges, sizeof(ranges)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | if (len <= 0) |
| 103 | return; |
| 104 | |
| 105 | len /= sizeof(struct linux_prom64_ranges); |
| 106 | for (i = 0; i < len; i++) { |
| 107 | struct linux_prom64_ranges *rng = &ranges[i]; |
| 108 | u64 phys_addr = regs->phys_addr; |
| 109 | |
| 110 | if (phys_addr >= rng->ot_child_base && |
| 111 | phys_addr < (rng->ot_child_base + rng->or_size)) { |
| 112 | regs->phys_addr -= rng->ot_child_base; |
| 113 | regs->phys_addr += rng->ot_parent_base; |
| 114 | return; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return; |
| 119 | } |
| 120 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 121 | static int ffb_init_one(drm_device_t * dev, int prom_node, int parent_node, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | int instance) |
| 123 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 124 | struct linux_prom64_registers regs[2 * PROMREG_MAX]; |
| 125 | ffb_dev_priv_t *ffb_priv = (ffb_dev_priv_t *) dev->dev_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | int i; |
| 127 | |
| 128 | ffb_priv->prom_node = prom_node; |
| 129 | if (prom_getproperty(ffb_priv->prom_node, "reg", |
| 130 | (void *)regs, sizeof(regs)) <= 0) { |
| 131 | return -EINVAL; |
| 132 | } |
| 133 | ffb_apply_upa_parent_ranges(parent_node, ®s[0]); |
| 134 | ffb_priv->card_phys_base = regs[0].phys_addr; |
| 135 | ffb_priv->regs = (ffb_fbcPtr) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 136 | (regs[0].phys_addr + 0x00600000UL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | get_ffb_type(ffb_priv, instance); |
| 138 | for (i = 0; i < FFB_MAX_CTXS; i++) |
| 139 | ffb_priv->hw_state[i] = NULL; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static drm_map_t *ffb_find_map(struct file *filp, unsigned long off) |
| 145 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 146 | drm_file_t *priv = filp->private_data; |
| 147 | drm_device_t *dev; |
| 148 | drm_map_list_t *r_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | struct list_head *list; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 150 | drm_map_t *map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
| 152 | if (!priv || (dev = priv->dev) == NULL) |
| 153 | return NULL; |
| 154 | |
| 155 | list_for_each(list, &dev->maplist->head) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 156 | r_list = (drm_map_list_t *) list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | map = r_list->map; |
| 158 | if (!map) |
| 159 | continue; |
Dave Airlie | d1f2b55 | 2005-08-05 22:11:22 +1000 | [diff] [blame] | 160 | if (r_list->user_token == off) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | return map; |
| 162 | } |
| 163 | |
| 164 | return NULL; |
| 165 | } |
| 166 | |
| 167 | unsigned long ffb_get_unmapped_area(struct file *filp, |
| 168 | unsigned long hint, |
| 169 | unsigned long len, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 170 | unsigned long pgoff, unsigned long flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | { |
| 172 | drm_map_t *map = ffb_find_map(filp, pgoff << PAGE_SHIFT); |
| 173 | unsigned long addr = -ENOMEM; |
| 174 | |
| 175 | if (!map) |
| 176 | return get_unmapped_area(NULL, hint, len, pgoff, flags); |
| 177 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 178 | if (map->type == _DRM_FRAME_BUFFER || map->type == _DRM_REGISTERS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | #ifdef HAVE_ARCH_FB_UNMAPPED_AREA |
| 180 | addr = get_fb_unmapped_area(filp, hint, len, pgoff, flags); |
| 181 | #else |
| 182 | addr = get_unmapped_area(NULL, hint, len, pgoff, flags); |
| 183 | #endif |
| 184 | } else if (map->type == _DRM_SHM && SHMLBA > PAGE_SIZE) { |
| 185 | unsigned long slack = SHMLBA - PAGE_SIZE; |
| 186 | |
| 187 | addr = get_unmapped_area(NULL, hint, len + slack, pgoff, flags); |
| 188 | if (!(addr & ~PAGE_MASK)) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 189 | unsigned long kvirt = (unsigned long)map->handle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
| 191 | if ((kvirt & (SHMLBA - 1)) != (addr & (SHMLBA - 1))) { |
| 192 | unsigned long koff, aoff; |
| 193 | |
| 194 | koff = kvirt & (SHMLBA - 1); |
| 195 | aoff = addr & (SHMLBA - 1); |
| 196 | if (koff < aoff) |
| 197 | koff += SHMLBA; |
| 198 | |
| 199 | addr += (koff - aoff); |
| 200 | } |
| 201 | } |
| 202 | } else { |
| 203 | addr = get_unmapped_area(NULL, hint, len, pgoff, flags); |
| 204 | } |
| 205 | |
| 206 | return addr; |
| 207 | } |
| 208 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 209 | static int ffb_presetup(drm_device_t * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 211 | ffb_dev_priv_t *ffb_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | int ret = 0; |
| 213 | int i = 0; |
| 214 | |
| 215 | /* Check for the case where no device was found. */ |
| 216 | if (ffb_position == NULL) |
| 217 | return -ENODEV; |
| 218 | |
| 219 | /* code used to use numdevs no numdevs anymore */ |
| 220 | ffb_priv = kmalloc(sizeof(ffb_dev_priv_t), GFP_KERNEL); |
| 221 | if (!ffb_priv) |
| 222 | return -ENOMEM; |
| 223 | memset(ffb_priv, 0, sizeof(*ffb_priv)); |
| 224 | dev->dev_private = ffb_priv; |
| 225 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 226 | ret = ffb_init_one(dev, ffb_position[i].node, ffb_position[i].root, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | return ret; |
| 228 | } |
| 229 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 230 | static void ffb_driver_release(drm_device_t * dev, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | { |
| 232 | ffb_dev_priv_t *fpriv = (ffb_dev_priv_t *) dev->dev_private; |
| 233 | int context = _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock); |
| 234 | int idx; |
| 235 | |
| 236 | idx = context - 1; |
| 237 | if (fpriv && |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 238 | context != DRM_KERNEL_CONTEXT && fpriv->hw_state[idx] != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | kfree(fpriv->hw_state[idx]); |
| 240 | fpriv->hw_state[idx] = NULL; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 241 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 244 | static void ffb_driver_pretakedown(drm_device_t * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | { |
Jesper Juhl | 735d566 | 2005-11-07 01:01:29 -0800 | [diff] [blame] | 246 | kfree(dev->dev_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 249 | static int ffb_driver_postcleanup(drm_device_t * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
Jesper Juhl | 735d566 | 2005-11-07 01:01:29 -0800 | [diff] [blame] | 251 | kfree(ffb_position); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | return 0; |
| 253 | } |
| 254 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 255 | static void ffb_driver_kernel_context_switch_unlock(struct drm_device *dev, |
| 256 | drm_lock_t * lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | { |
| 258 | dev->lock.filp = 0; |
| 259 | { |
| 260 | __volatile__ unsigned int *plock = &dev->lock.hw_lock->lock; |
| 261 | unsigned int old, new, prev, ctx; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | ctx = lock->context; |
| 264 | do { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 265 | old = *plock; |
| 266 | new = ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | prev = cmpxchg(plock, old, new); |
| 268 | } while (prev != old); |
| 269 | } |
| 270 | wake_up_interruptible(&dev->lock.lock_queue); |
| 271 | } |
| 272 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 273 | static unsigned long ffb_driver_get_map_ofs(drm_map_t * map) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | { |
| 275 | return (map->offset & 0xffffffff); |
| 276 | } |
| 277 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 278 | static unsigned long ffb_driver_get_reg_ofs(drm_device_t * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 280 | ffb_dev_priv_t *ffb_priv = (ffb_dev_priv_t *) dev->dev_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 282 | if (ffb_priv) |
| 283 | return ffb_priv->card_phys_base; |
| 284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | return 0; |
| 286 | } |
| 287 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 288 | static int postinit(struct drm_device *dev, unsigned long flags) |
| 289 | { |
| 290 | DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n", |
| 291 | DRIVER_NAME, |
| 292 | DRIVER_MAJOR, |
| 293 | DRIVER_MINOR, DRIVER_PATCHLEVEL, DRIVER_DATE, dev->minor); |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static int version(drm_version_t * version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | { |
| 299 | int len; |
| 300 | |
| 301 | version->version_major = DRIVER_MAJOR; |
| 302 | version->version_minor = DRIVER_MINOR; |
| 303 | version->version_patchlevel = DRIVER_PATCHLEVEL; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 304 | DRM_COPY(version->name, DRIVER_NAME); |
| 305 | DRM_COPY(version->date, DRIVER_DATE); |
| 306 | DRM_COPY(version->desc, DRIVER_DESC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static drm_ioctl_desc_t ioctls[] = { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | }; |
| 313 | |
| 314 | static struct drm_driver driver = { |
| 315 | .driver_features = 0, |
| 316 | .dev_priv_size = sizeof(u32), |
| 317 | .release = ffb_driver_release, |
| 318 | .presetup = ffb_presetup, |
| 319 | .pretakedown = ffb_driver_pretakedown, |
| 320 | .postcleanup = ffb_driver_postcleanup, |
| 321 | .kernel_context_switch = ffb_driver_context_switch, |
| 322 | .kernel_context_switch_unlock = ffb_driver_kernel_context_switch_unlock, |
| 323 | .get_map_ofs = ffb_driver_get_map_ofs, |
| 324 | .get_reg_ofs = ffb_driver_get_reg_ofs, |
| 325 | .postinit = postinit, |
| 326 | .version = version, |
| 327 | .ioctls = ioctls, |
| 328 | .num_ioctls = DRM_ARRAY_SIZE(ioctls), |
| 329 | .fops = { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 330 | .owner = THIS_MODULE, |
| 331 | .open = drm_open, |
| 332 | .release = drm_release, |
| 333 | .ioctl = drm_ioctl, |
| 334 | .mmap = drm_mmap, |
| 335 | .poll = drm_poll, |
| 336 | .fasync = drm_fasync, |
| 337 | } |
| 338 | , |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | }; |
| 340 | |
| 341 | static int __init ffb_init(void) |
| 342 | { |
| 343 | return -ENODEV; |
| 344 | } |
| 345 | |
| 346 | static void __exit ffb_exit(void) |
| 347 | { |
| 348 | } |
| 349 | |
| 350 | module_init(ffb_init); |
| 351 | module_exit(ffb_exit); |
| 352 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 353 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 354 | MODULE_DESCRIPTION(DRIVER_DESC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | MODULE_LICENSE("GPL and additional rights"); |