[PATCH] ppc64 iSeries: make virtual DVD-RAMs writable again
[linux-2.6] / drivers / char / drm / ffb_drv.c
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
36 static void get_ffb_type(ffb_dev_priv_t *ffb_priv, int instance)
37 {
38         volatile unsigned char *strap_bits;
39         unsigned char val;
40
41         strap_bits = (volatile unsigned char *)
42                 (ffb_priv->card_phys_base + 0x00200000UL);
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;
64                 printk("ffb%d: Detected FFB2/vertical pre-FCS prototype\n", instance);
65                 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;
84                 printk("ffb%d: Unknown boardID[%08x], assuming FFB2\n", instance, val);
85                 break;
86         };
87 }
88
89 static void ffb_apply_upa_parent_ranges(int parent, 
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
100         len = prom_getproperty(parent, "ranges", (void *) ranges, sizeof(ranges));
101         if (len <= 0)
102                 return;
103
104         len /= sizeof(struct linux_prom64_ranges);
105         for (i = 0; i < len; i++) {
106                 struct linux_prom64_ranges *rng = &ranges[i];
107                 u64 phys_addr = regs->phys_addr;
108
109                 if (phys_addr >= rng->ot_child_base &&
110                     phys_addr < (rng->ot_child_base + rng->or_size)) {
111                         regs->phys_addr -= rng->ot_child_base;
112                         regs->phys_addr += rng->ot_parent_base;
113                         return;
114                 }
115         }
116
117         return;
118 }
119
120 static int ffb_init_one(drm_device_t *dev, int prom_node, int parent_node,
121                         int instance)
122 {
123         struct linux_prom64_registers regs[2*PROMREG_MAX];
124         ffb_dev_priv_t *ffb_priv = (ffb_dev_priv_t *)dev->dev_private;
125         int i;
126
127         ffb_priv->prom_node = prom_node;
128         if (prom_getproperty(ffb_priv->prom_node, "reg",
129                              (void *)regs, sizeof(regs)) <= 0) {
130                 return -EINVAL;
131         }
132         ffb_apply_upa_parent_ranges(parent_node, &regs[0]);
133         ffb_priv->card_phys_base = regs[0].phys_addr;
134         ffb_priv->regs = (ffb_fbcPtr)
135                 (regs[0].phys_addr + 0x00600000UL);
136         get_ffb_type(ffb_priv, instance);
137         for (i = 0; i < FFB_MAX_CTXS; i++)
138                 ffb_priv->hw_state[i] = NULL;
139         
140         return 0;
141 }
142
143 static drm_map_t *ffb_find_map(struct file *filp, unsigned long off)
144 {
145         drm_file_t      *priv   = filp->private_data;
146         drm_device_t    *dev;
147         drm_map_list_t  *r_list;
148         struct list_head *list;
149         drm_map_t       *map;
150
151         if (!priv || (dev = priv->dev) == NULL)
152                 return NULL;
153
154         list_for_each(list, &dev->maplist->head) {
155                 unsigned long uoff;
156
157                 r_list = (drm_map_list_t *)list;
158                 map = r_list->map;
159                 if (!map)
160                         continue;
161                 uoff = (map->offset & 0xffffffff);
162                 if (uoff == off)
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,
172                                     unsigned long pgoff,
173                                     unsigned long flags)
174 {
175         drm_map_t *map = ffb_find_map(filp, pgoff << PAGE_SHIFT);
176         unsigned long addr = -ENOMEM;
177
178         if (!map)
179                 return get_unmapped_area(NULL, hint, len, pgoff, flags);
180
181         if (map->type == _DRM_FRAME_BUFFER ||
182             map->type == _DRM_REGISTERS) {
183 #ifdef HAVE_ARCH_FB_UNMAPPED_AREA
184                 addr = get_fb_unmapped_area(filp, hint, len, pgoff, flags);
185 #else
186                 addr = get_unmapped_area(NULL, hint, len, pgoff, flags);
187 #endif
188         } else if (map->type == _DRM_SHM && SHMLBA > PAGE_SIZE) {
189                 unsigned long slack = SHMLBA - PAGE_SIZE;
190
191                 addr = get_unmapped_area(NULL, hint, len + slack, pgoff, flags);
192                 if (!(addr & ~PAGE_MASK)) {
193                         unsigned long kvirt = (unsigned long) map->handle;
194
195                         if ((kvirt & (SHMLBA - 1)) != (addr & (SHMLBA - 1))) {
196                                 unsigned long koff, aoff;
197
198                                 koff = kvirt & (SHMLBA - 1);
199                                 aoff = addr & (SHMLBA - 1);
200                                 if (koff < aoff)
201                                         koff += SHMLBA;
202
203                                 addr += (koff - aoff);
204                         }
205                 }
206         } else {
207                 addr = get_unmapped_area(NULL, hint, len, pgoff, flags);
208         }
209
210         return addr;
211 }
212
213 static int ffb_presetup(drm_device_t *dev)
214 {
215         ffb_dev_priv_t  *ffb_priv;
216         int ret = 0;
217         int i = 0;
218
219         /* Check for the case where no device was found. */
220         if (ffb_position == NULL)
221                 return -ENODEV;
222
223         /* code used to use numdevs no numdevs anymore */
224         ffb_priv = kmalloc(sizeof(ffb_dev_priv_t), GFP_KERNEL);
225         if (!ffb_priv)
226                 return -ENOMEM;
227         memset(ffb_priv, 0, sizeof(*ffb_priv));
228         dev->dev_private = ffb_priv;
229
230         ret = ffb_init_one(dev,
231                            ffb_position[i].node,
232                            ffb_position[i].root,
233                            i);
234         return ret;
235 }
236
237 static void ffb_driver_release(drm_device_t *dev, struct file *filp)
238 {
239         ffb_dev_priv_t *fpriv = (ffb_dev_priv_t *) dev->dev_private;
240         int context = _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock);
241         int idx;
242
243         idx = context - 1;
244         if (fpriv &&
245             context != DRM_KERNEL_CONTEXT &&
246             fpriv->hw_state[idx] != NULL) {
247                 kfree(fpriv->hw_state[idx]);
248                 fpriv->hw_state[idx] = NULL;
249         }       
250 }
251
252 static void ffb_driver_pretakedown(drm_device_t *dev)
253 {
254         if (dev->dev_private) kfree(dev->dev_private);
255 }
256
257 static int ffb_driver_postcleanup(drm_device_t *dev)
258 {
259         if (ffb_position != NULL) kfree(ffb_position);
260         return 0;
261 }
262
263 static void ffb_driver_kernel_context_switch_unlock(struct drm_device *dev, drm_lock_t *lock)
264 {
265         dev->lock.filp = 0;
266         {
267                 __volatile__ unsigned int *plock = &dev->lock.hw_lock->lock;
268                 unsigned int old, new, prev, ctx;
269                 
270                 ctx = lock->context;
271                 do {
272                         old  = *plock;
273                         new  = ctx;
274                         prev = cmpxchg(plock, old, new);
275                 } while (prev != old);
276         }
277         wake_up_interruptible(&dev->lock.lock_queue);
278 }
279
280 static unsigned long ffb_driver_get_map_ofs(drm_map_t *map)
281 {
282         return (map->offset & 0xffffffff);
283 }
284
285 static unsigned long ffb_driver_get_reg_ofs(drm_device_t *dev)
286 {
287        ffb_dev_priv_t *ffb_priv = (ffb_dev_priv_t *)dev->dev_private;
288        
289        if (ffb_priv)
290                return ffb_priv->card_phys_base;
291        
292        return 0;
293 }
294
295 static int postinit( struct drm_device *dev, unsigned long flags ) 
296 {
297         DRM_INFO( "Initialized %s %d.%d.%d %s on minor %d\n",
298                 DRIVER_NAME,
299                 DRIVER_MAJOR,
300                 DRIVER_MINOR,
301                 DRIVER_PATCHLEVEL,
302                 DRIVER_DATE,
303                 dev->minor
304                 );
305         return 0;
306 }
307
308 static int version( drm_version_t *version )
309 {
310         int len;
311
312         version->version_major = DRIVER_MAJOR;
313         version->version_minor = DRIVER_MINOR;
314         version->version_patchlevel = DRIVER_PATCHLEVEL;
315         DRM_COPY( version->name, DRIVER_NAME );
316         DRM_COPY( version->date, DRIVER_DATE );
317         DRM_COPY( version->desc, DRIVER_DESC );
318         return 0;
319 }
320
321 static drm_ioctl_desc_t ioctls[] = {
322         
323 };
324
325 static struct drm_driver driver = {
326         .driver_features = 0,
327         .dev_priv_size = sizeof(u32),
328         .release = ffb_driver_release,
329         .presetup = ffb_presetup,
330         .pretakedown = ffb_driver_pretakedown,
331         .postcleanup = ffb_driver_postcleanup,
332         .kernel_context_switch = ffb_driver_context_switch,
333         .kernel_context_switch_unlock = ffb_driver_kernel_context_switch_unlock,
334         .get_map_ofs = ffb_driver_get_map_ofs,
335         .get_reg_ofs = ffb_driver_get_reg_ofs,
336         .postinit = postinit,
337         .version = version,
338         .ioctls = ioctls,
339         .num_ioctls = DRM_ARRAY_SIZE(ioctls),
340         .fops = {
341                 .owner = THIS_MODULE,
342                 .open = drm_open,
343                 .release = drm_release,
344                 .ioctl = drm_ioctl,
345                 .mmap = drm_mmap,
346                 .poll = drm_poll,
347                 .fasync = drm_fasync,
348         },
349 };
350
351 static int __init ffb_init(void)
352 {
353         return -ENODEV;
354 }
355
356 static void __exit ffb_exit(void)
357 {
358 }
359
360 module_init(ffb_init);
361 module_exit(ffb_exit);
362
363 MODULE_AUTHOR( DRIVER_AUTHOR );
364 MODULE_DESCRIPTION( DRIVER_DESC );
365 MODULE_LICENSE("GPL and additional rights");