blob: dd45111a48547619063d30dfdadd2eb2c78a81ba [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $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 Torvalds1da177e2005-04-16 15:20:36 -07007#include "ffb.h"
8#include "drmP.h"
9
10#include "ffb_drv.h"
11
12#include <linux/sched.h>
13#include <linux/smp_lock.h>
14#include <asm/shmparam.h>
15#include <asm/oplib.h>
16#include <asm/upa.h>
17
18#define DRIVER_AUTHOR "David S. Miller"
19
20#define DRIVER_NAME "ffb"
21#define DRIVER_DESC "Creator/Creator3D"
22#define DRIVER_DATE "20000517"
23
24#define DRIVER_MAJOR 0
25#define DRIVER_MINOR 0
26#define DRIVER_PATCHLEVEL 1
27
28typedef struct _ffb_position_t {
29 int node;
30 int root;
31} ffb_position_t;
32
33static ffb_position_t *ffb_position;
34
Dave Airlieb5e89ed2005-09-25 14:28:13 +100035static void get_ffb_type(ffb_dev_priv_t * ffb_priv, int instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
37 volatile unsigned char *strap_bits;
38 unsigned char val;
39
40 strap_bits = (volatile unsigned char *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +100041 (ffb_priv->card_phys_base + 0x00200000UL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43 /* Don't ask, you have to read the value twice for whatever
44 * reason to get correct contents.
45 */
46 val = upa_readb(strap_bits);
47 val = upa_readb(strap_bits);
48 switch (val & 0x78) {
49 case (0x0 << 5) | (0x0 << 3):
50 ffb_priv->ffb_type = ffb1_prototype;
51 printk("ffb%d: Detected FFB1 pre-FCS prototype\n", instance);
52 break;
53 case (0x0 << 5) | (0x1 << 3):
54 ffb_priv->ffb_type = ffb1_standard;
55 printk("ffb%d: Detected FFB1\n", instance);
56 break;
57 case (0x0 << 5) | (0x3 << 3):
58 ffb_priv->ffb_type = ffb1_speedsort;
59 printk("ffb%d: Detected FFB1-SpeedSort\n", instance);
60 break;
61 case (0x1 << 5) | (0x0 << 3):
62 ffb_priv->ffb_type = ffb2_prototype;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100063 printk("ffb%d: Detected FFB2/vertical pre-FCS prototype\n",
64 instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 break;
66 case (0x1 << 5) | (0x1 << 3):
67 ffb_priv->ffb_type = ffb2_vertical;
68 printk("ffb%d: Detected FFB2/vertical\n", instance);
69 break;
70 case (0x1 << 5) | (0x2 << 3):
71 ffb_priv->ffb_type = ffb2_vertical_plus;
72 printk("ffb%d: Detected FFB2+/vertical\n", instance);
73 break;
74 case (0x2 << 5) | (0x0 << 3):
75 ffb_priv->ffb_type = ffb2_horizontal;
76 printk("ffb%d: Detected FFB2/horizontal\n", instance);
77 break;
78 case (0x2 << 5) | (0x2 << 3):
79 ffb_priv->ffb_type = ffb2_horizontal;
80 printk("ffb%d: Detected FFB2+/horizontal\n", instance);
81 break;
82 default:
83 ffb_priv->ffb_type = ffb2_vertical;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100084 printk("ffb%d: Unknown boardID[%08x], assuming FFB2\n",
85 instance, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 break;
87 };
88}
89
Dave Airlieb5e89ed2005-09-25 14:28:13 +100090static void ffb_apply_upa_parent_ranges(int parent,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 struct linux_prom64_registers *regs)
92{
93 struct linux_prom64_ranges ranges[PROMREG_MAX];
94 char name[128];
95 int len, i;
96
97 prom_getproperty(parent, "name", name, sizeof(name));
98 if (strcmp(name, "upa") != 0)
99 return;
100
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000101 len =
102 prom_getproperty(parent, "ranges", (void *)ranges, sizeof(ranges));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (len <= 0)
104 return;
105
106 len /= sizeof(struct linux_prom64_ranges);
107 for (i = 0; i < len; i++) {
108 struct linux_prom64_ranges *rng = &ranges[i];
109 u64 phys_addr = regs->phys_addr;
110
111 if (phys_addr >= rng->ot_child_base &&
112 phys_addr < (rng->ot_child_base + rng->or_size)) {
113 regs->phys_addr -= rng->ot_child_base;
114 regs->phys_addr += rng->ot_parent_base;
115 return;
116 }
117 }
118
119 return;
120}
121
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000122static int ffb_init_one(drm_device_t * dev, int prom_node, int parent_node,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 int instance)
124{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000125 struct linux_prom64_registers regs[2 * PROMREG_MAX];
126 ffb_dev_priv_t *ffb_priv = (ffb_dev_priv_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 int i;
128
129 ffb_priv->prom_node = prom_node;
130 if (prom_getproperty(ffb_priv->prom_node, "reg",
131 (void *)regs, sizeof(regs)) <= 0) {
132 return -EINVAL;
133 }
134 ffb_apply_upa_parent_ranges(parent_node, &regs[0]);
135 ffb_priv->card_phys_base = regs[0].phys_addr;
136 ffb_priv->regs = (ffb_fbcPtr)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000137 (regs[0].phys_addr + 0x00600000UL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 get_ffb_type(ffb_priv, instance);
139 for (i = 0; i < FFB_MAX_CTXS; i++)
140 ffb_priv->hw_state[i] = NULL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return 0;
143}
144
145static drm_map_t *ffb_find_map(struct file *filp, unsigned long off)
146{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000147 drm_file_t *priv = filp->private_data;
148 drm_device_t *dev;
149 drm_map_list_t *r_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 struct list_head *list;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000151 drm_map_t *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 if (!priv || (dev = priv->dev) == NULL)
154 return NULL;
155
156 list_for_each(list, &dev->maplist->head) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000157 r_list = (drm_map_list_t *) list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 map = r_list->map;
159 if (!map)
160 continue;
Dave Airlied1f2b552005-08-05 22:11:22 +1000161 if (r_list->user_token == off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return map;
163 }
164
165 return NULL;
166}
167
168unsigned long ffb_get_unmapped_area(struct file *filp,
169 unsigned long hint,
170 unsigned long len,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000171 unsigned long pgoff, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 drm_map_t *map = ffb_find_map(filp, pgoff << PAGE_SHIFT);
174 unsigned long addr = -ENOMEM;
175
176 if (!map)
177 return get_unmapped_area(NULL, hint, len, pgoff, flags);
178
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000179 if (map->type == _DRM_FRAME_BUFFER || map->type == _DRM_REGISTERS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180#ifdef HAVE_ARCH_FB_UNMAPPED_AREA
181 addr = get_fb_unmapped_area(filp, hint, len, pgoff, flags);
182#else
183 addr = get_unmapped_area(NULL, hint, len, pgoff, flags);
184#endif
185 } else if (map->type == _DRM_SHM && SHMLBA > PAGE_SIZE) {
186 unsigned long slack = SHMLBA - PAGE_SIZE;
187
188 addr = get_unmapped_area(NULL, hint, len + slack, pgoff, flags);
189 if (!(addr & ~PAGE_MASK)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000190 unsigned long kvirt = (unsigned long)map->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 if ((kvirt & (SHMLBA - 1)) != (addr & (SHMLBA - 1))) {
193 unsigned long koff, aoff;
194
195 koff = kvirt & (SHMLBA - 1);
196 aoff = addr & (SHMLBA - 1);
197 if (koff < aoff)
198 koff += SHMLBA;
199
200 addr += (koff - aoff);
201 }
202 }
203 } else {
204 addr = get_unmapped_area(NULL, hint, len, pgoff, flags);
205 }
206
207 return addr;
208}
209
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000210static int ffb_presetup(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000212 ffb_dev_priv_t *ffb_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 int ret = 0;
214 int i = 0;
215
216 /* Check for the case where no device was found. */
217 if (ffb_position == NULL)
218 return -ENODEV;
219
220 /* code used to use numdevs no numdevs anymore */
221 ffb_priv = kmalloc(sizeof(ffb_dev_priv_t), GFP_KERNEL);
222 if (!ffb_priv)
223 return -ENOMEM;
224 memset(ffb_priv, 0, sizeof(*ffb_priv));
225 dev->dev_private = ffb_priv;
226
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000227 ret = ffb_init_one(dev, ffb_position[i].node, ffb_position[i].root, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return ret;
229}
230
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000231static void ffb_driver_release(drm_device_t * dev, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 ffb_dev_priv_t *fpriv = (ffb_dev_priv_t *) dev->dev_private;
234 int context = _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock);
235 int idx;
236
237 idx = context - 1;
238 if (fpriv &&
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000239 context != DRM_KERNEL_CONTEXT && fpriv->hw_state[idx] != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 kfree(fpriv->hw_state[idx]);
241 fpriv->hw_state[idx] = NULL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000245static void ffb_driver_pretakedown(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Jesper Juhl735d5662005-11-07 01:01:29 -0800247 kfree(dev->dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000250static int ffb_driver_postcleanup(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Jesper Juhl735d5662005-11-07 01:01:29 -0800252 kfree(ffb_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return 0;
254}
255
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000256static void ffb_driver_kernel_context_switch_unlock(struct drm_device *dev,
257 drm_lock_t * lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 dev->lock.filp = 0;
260 {
261 __volatile__ unsigned int *plock = &dev->lock.hw_lock->lock;
262 unsigned int old, new, prev, ctx;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 ctx = lock->context;
265 do {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000266 old = *plock;
267 new = ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 prev = cmpxchg(plock, old, new);
269 } while (prev != old);
270 }
271 wake_up_interruptible(&dev->lock.lock_queue);
272}
273
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000274static unsigned long ffb_driver_get_map_ofs(drm_map_t * map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 return (map->offset & 0xffffffff);
277}
278
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000279static unsigned long ffb_driver_get_reg_ofs(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000281 ffb_dev_priv_t *ffb_priv = (ffb_dev_priv_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000283 if (ffb_priv)
284 return ffb_priv->card_phys_base;
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return 0;
287}
288
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000289static int postinit(struct drm_device *dev, unsigned long flags)
290{
291 DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n",
292 DRIVER_NAME,
293 DRIVER_MAJOR,
294 DRIVER_MINOR, DRIVER_PATCHLEVEL, DRIVER_DATE, dev->minor);
295 return 0;
296}
297
298static int version(drm_version_t * version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 int len;
301
302 version->version_major = DRIVER_MAJOR;
303 version->version_minor = DRIVER_MINOR;
304 version->version_patchlevel = DRIVER_PATCHLEVEL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000305 DRM_COPY(version->name, DRIVER_NAME);
306 DRM_COPY(version->date, DRIVER_DATE);
307 DRM_COPY(version->desc, DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return 0;
309}
310
311static drm_ioctl_desc_t ioctls[] = {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313};
314
315static struct drm_driver driver = {
316 .driver_features = 0,
317 .dev_priv_size = sizeof(u32),
318 .release = ffb_driver_release,
319 .presetup = ffb_presetup,
320 .pretakedown = ffb_driver_pretakedown,
321 .postcleanup = ffb_driver_postcleanup,
322 .kernel_context_switch = ffb_driver_context_switch,
323 .kernel_context_switch_unlock = ffb_driver_kernel_context_switch_unlock,
324 .get_map_ofs = ffb_driver_get_map_ofs,
325 .get_reg_ofs = ffb_driver_get_reg_ofs,
326 .postinit = postinit,
327 .version = version,
328 .ioctls = ioctls,
329 .num_ioctls = DRM_ARRAY_SIZE(ioctls),
330 .fops = {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000331 .owner = THIS_MODULE,
332 .open = drm_open,
333 .release = drm_release,
334 .ioctl = drm_ioctl,
335 .mmap = drm_mmap,
336 .poll = drm_poll,
337 .fasync = drm_fasync,
338 }
339 ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340};
341
342static int __init ffb_init(void)
343{
344 return -ENODEV;
345}
346
347static void __exit ffb_exit(void)
348{
349}
350
351module_init(ffb_init);
352module_exit(ffb_exit);
353
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000354MODULE_AUTHOR(DRIVER_AUTHOR);
355MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356MODULE_LICENSE("GPL and additional rights");