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