drm/ast: Don't assume DVO enabled means SIL164 on uninitialized chips
[cascardo/linux.git] / drivers / gpu / drm / ast / ast_main.c
1 /*
2  * Copyright 2012 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sub license, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
15  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
16  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18  * USE OR OTHER DEALINGS IN THE SOFTWARE.
19  *
20  * The above copyright notice and this permission notice (including the
21  * next paragraph) shall be included in all copies or substantial portions
22  * of the Software.
23  *
24  */
25 /*
26  * Authors: Dave Airlie <airlied@redhat.com>
27  */
28 #include <drm/drmP.h>
29 #include "ast_drv.h"
30
31
32 #include <drm/drm_fb_helper.h>
33 #include <drm/drm_crtc_helper.h>
34
35 #include "ast_dram_tables.h"
36
37 void ast_set_index_reg_mask(struct ast_private *ast,
38                             uint32_t base, uint8_t index,
39                             uint8_t mask, uint8_t val)
40 {
41         u8 tmp;
42         ast_io_write8(ast, base, index);
43         tmp = (ast_io_read8(ast, base + 1) & mask) | val;
44         ast_set_index_reg(ast, base, index, tmp);
45 }
46
47 uint8_t ast_get_index_reg(struct ast_private *ast,
48                           uint32_t base, uint8_t index)
49 {
50         uint8_t ret;
51         ast_io_write8(ast, base, index);
52         ret = ast_io_read8(ast, base + 1);
53         return ret;
54 }
55
56 uint8_t ast_get_index_reg_mask(struct ast_private *ast,
57                                uint32_t base, uint8_t index, uint8_t mask)
58 {
59         uint8_t ret;
60         ast_io_write8(ast, base, index);
61         ret = ast_io_read8(ast, base + 1) & mask;
62         return ret;
63 }
64
65
66 static int ast_detect_chip(struct drm_device *dev, bool *need_post)
67 {
68         struct ast_private *ast = dev->dev_private;
69         uint32_t data, jreg;
70
71         if (dev->pdev->device == PCI_CHIP_AST1180) {
72                 ast->chip = AST1100;
73                 DRM_INFO("AST 1180 detected\n");
74         } else {
75                 if (dev->pdev->revision >= 0x30) {
76                         ast->chip = AST2400;
77                         DRM_INFO("AST 2400 detected\n");
78                 } else if (dev->pdev->revision >= 0x20) {
79                         ast->chip = AST2300;
80                         DRM_INFO("AST 2300 detected\n");
81                 } else if (dev->pdev->revision >= 0x10) {
82                         uint32_t data;
83                         ast_write32(ast, 0xf004, 0x1e6e0000);
84                         ast_write32(ast, 0xf000, 0x1);
85
86                         data = ast_read32(ast, 0x1207c);
87                         switch (data & 0x0300) {
88                         case 0x0200:
89                                 ast->chip = AST1100;
90                                 DRM_INFO("AST 1100 detected\n");
91                                 break;
92                         case 0x0100:
93                                 ast->chip = AST2200;
94                                 DRM_INFO("AST 2200 detected\n");
95                                 break;
96                         case 0x0000:
97                                 ast->chip = AST2150;
98                                 DRM_INFO("AST 2150 detected\n");
99                                 break;
100                         default:
101                                 ast->chip = AST2100;
102                                 DRM_INFO("AST 2100 detected\n");
103                                 break;
104                         }
105                         ast->vga2_clone = false;
106                 } else {
107                         ast->chip = 2000;
108                         DRM_INFO("AST 2000 detected\n");
109                 }
110         }
111
112         /*
113          * If VGA isn't enabled, we need to enable now or subsequent
114          * access to the scratch registers will fail. We also inform
115          * our caller that it needs to POST the chip
116          * (Assumption: VGA not enabled -> need to POST)
117          */
118         if (!ast_is_vga_enabled(dev)) {
119                 ast_enable_vga(dev);
120                 ast_enable_mmio(dev);
121                 DRM_INFO("VGA not enabled on entry, requesting chip POST\n");
122                 *need_post = true;
123         } else
124                 *need_post = false;
125
126         /* Check if we support wide screen */
127         switch (ast->chip) {
128         case AST1180:
129                 ast->support_wide_screen = true;
130                 break;
131         case AST2000:
132                 ast->support_wide_screen = false;
133                 break;
134         default:
135                 jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd0, 0xff);
136                 if (!(jreg & 0x80))
137                         ast->support_wide_screen = true;
138                 else if (jreg & 0x01)
139                         ast->support_wide_screen = true;
140                 else {
141                         ast->support_wide_screen = false;
142                         /* Read SCU7c (silicon revision register) */
143                         ast_write32(ast, 0xf004, 0x1e6e0000);
144                         ast_write32(ast, 0xf000, 0x1);
145                         data = ast_read32(ast, 0x1207c);
146                         data &= 0x300;
147                         if (ast->chip == AST2300 && data == 0x0) /* ast1300 */
148                                 ast->support_wide_screen = true;
149                         if (ast->chip == AST2400 && data == 0x100) /* ast1400 */
150                                 ast->support_wide_screen = true;
151                 }
152                 break;
153         }
154
155         /* Check 3rd Tx option (digital output afaik) */
156         ast->tx_chip_type = AST_TX_NONE;
157
158         /*
159          * VGACRA3 Enhanced Color Mode Register, check if DVO is already
160          * enabled, in that case, assume we have a SIL164 TMDS transmitter
161          *
162          * Don't make that assumption if we the chip wasn't enabled and
163          * is at power-on reset, otherwise we'll incorrectly "detect" a
164          * SIL164 when there is none.
165          */
166         if (!*need_post) {
167                 jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa3, 0xff);
168                 if (jreg & 0x80)
169                         ast->tx_chip_type = AST_TX_SIL164;
170         }
171
172         if ((ast->chip == AST2300) || (ast->chip == AST2400)) {
173                 /*
174                  * On AST2300 and 2400, look the configuration set by the SoC in
175                  * the SOC scratch register #1 bits 11:8 (interestingly marked
176                  * as "reserved" in the spec)
177                  */
178                 jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd1, 0xff);
179                 switch (jreg) {
180                 case 0x04:
181                         ast->tx_chip_type = AST_TX_SIL164;
182                         break;
183                 case 0x08:
184                         ast->dp501_fw_addr = kzalloc(32*1024, GFP_KERNEL);
185                         if (ast->dp501_fw_addr) {
186                                 /* backup firmware */
187                                 if (ast_backup_fw(dev, ast->dp501_fw_addr, 32*1024)) {
188                                         kfree(ast->dp501_fw_addr);
189                                         ast->dp501_fw_addr = NULL;
190                                 }
191                         }
192                         /* fallthrough */
193                 case 0x0c:
194                         ast->tx_chip_type = AST_TX_DP501;
195                 }
196         }
197
198         /* Print stuff for diagnostic purposes */
199         switch(ast->tx_chip_type) {
200         case AST_TX_SIL164:
201                 DRM_INFO("Using Sil164 TMDS transmitter\n");
202                 break;
203         case AST_TX_DP501:
204                 DRM_INFO("Using DP501 DisplayPort transmitter\n");
205                 break;
206         default:
207                 DRM_INFO("Analog VGA only\n");
208         }
209         return 0;
210 }
211
212 static int ast_get_dram_info(struct drm_device *dev)
213 {
214         struct ast_private *ast = dev->dev_private;
215         uint32_t data, data2;
216         uint32_t denum, num, div, ref_pll;
217
218         ast_write32(ast, 0xf004, 0x1e6e0000);
219         ast_write32(ast, 0xf000, 0x1);
220
221
222         ast_write32(ast, 0x10000, 0xfc600309);
223
224         do {
225                 ;
226         } while (ast_read32(ast, 0x10000) != 0x01);
227         data = ast_read32(ast, 0x10004);
228
229         if (data & 0x400)
230                 ast->dram_bus_width = 16;
231         else
232                 ast->dram_bus_width = 32;
233
234         if (ast->chip == AST2300 || ast->chip == AST2400) {
235                 switch (data & 0x03) {
236                 case 0:
237                         ast->dram_type = AST_DRAM_512Mx16;
238                         break;
239                 default:
240                 case 1:
241                         ast->dram_type = AST_DRAM_1Gx16;
242                         break;
243                 case 2:
244                         ast->dram_type = AST_DRAM_2Gx16;
245                         break;
246                 case 3:
247                         ast->dram_type = AST_DRAM_4Gx16;
248                         break;
249                 }
250         } else {
251                 switch (data & 0x0c) {
252                 case 0:
253                 case 4:
254                         ast->dram_type = AST_DRAM_512Mx16;
255                         break;
256                 case 8:
257                         if (data & 0x40)
258                                 ast->dram_type = AST_DRAM_1Gx16;
259                         else
260                                 ast->dram_type = AST_DRAM_512Mx32;
261                         break;
262                 case 0xc:
263                         ast->dram_type = AST_DRAM_1Gx32;
264                         break;
265                 }
266         }
267
268         data = ast_read32(ast, 0x10120);
269         data2 = ast_read32(ast, 0x10170);
270         if (data2 & 0x2000)
271                 ref_pll = 14318;
272         else
273                 ref_pll = 12000;
274
275         denum = data & 0x1f;
276         num = (data & 0x3fe0) >> 5;
277         data = (data & 0xc000) >> 14;
278         switch (data) {
279         case 3:
280                 div = 0x4;
281                 break;
282         case 2:
283         case 1:
284                 div = 0x2;
285                 break;
286         default:
287                 div = 0x1;
288                 break;
289         }
290         ast->mclk = ref_pll * (num + 2) / (denum + 2) * (div * 1000);
291         return 0;
292 }
293
294 static void ast_user_framebuffer_destroy(struct drm_framebuffer *fb)
295 {
296         struct ast_framebuffer *ast_fb = to_ast_framebuffer(fb);
297         if (ast_fb->obj)
298                 drm_gem_object_unreference_unlocked(ast_fb->obj);
299
300         drm_framebuffer_cleanup(fb);
301         kfree(fb);
302 }
303
304 static const struct drm_framebuffer_funcs ast_fb_funcs = {
305         .destroy = ast_user_framebuffer_destroy,
306 };
307
308
309 int ast_framebuffer_init(struct drm_device *dev,
310                          struct ast_framebuffer *ast_fb,
311                          struct drm_mode_fb_cmd2 *mode_cmd,
312                          struct drm_gem_object *obj)
313 {
314         int ret;
315
316         drm_helper_mode_fill_fb_struct(&ast_fb->base, mode_cmd);
317         ast_fb->obj = obj;
318         ret = drm_framebuffer_init(dev, &ast_fb->base, &ast_fb_funcs);
319         if (ret) {
320                 DRM_ERROR("framebuffer init failed %d\n", ret);
321                 return ret;
322         }
323         return 0;
324 }
325
326 static struct drm_framebuffer *
327 ast_user_framebuffer_create(struct drm_device *dev,
328                struct drm_file *filp,
329                struct drm_mode_fb_cmd2 *mode_cmd)
330 {
331         struct drm_gem_object *obj;
332         struct ast_framebuffer *ast_fb;
333         int ret;
334
335         obj = drm_gem_object_lookup(dev, filp, mode_cmd->handles[0]);
336         if (obj == NULL)
337                 return ERR_PTR(-ENOENT);
338
339         ast_fb = kzalloc(sizeof(*ast_fb), GFP_KERNEL);
340         if (!ast_fb) {
341                 drm_gem_object_unreference_unlocked(obj);
342                 return ERR_PTR(-ENOMEM);
343         }
344
345         ret = ast_framebuffer_init(dev, ast_fb, mode_cmd, obj);
346         if (ret) {
347                 drm_gem_object_unreference_unlocked(obj);
348                 kfree(ast_fb);
349                 return ERR_PTR(ret);
350         }
351         return &ast_fb->base;
352 }
353
354 static const struct drm_mode_config_funcs ast_mode_funcs = {
355         .fb_create = ast_user_framebuffer_create,
356 };
357
358 static u32 ast_get_vram_info(struct drm_device *dev)
359 {
360         struct ast_private *ast = dev->dev_private;
361         u8 jreg;
362         u32 vram_size;
363         ast_open_key(ast);
364
365         vram_size = AST_VIDMEM_DEFAULT_SIZE;
366         jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xaa, 0xff);
367         switch (jreg & 3) {
368         case 0: vram_size = AST_VIDMEM_SIZE_8M; break;
369         case 1: vram_size = AST_VIDMEM_SIZE_16M; break;
370         case 2: vram_size = AST_VIDMEM_SIZE_32M; break;
371         case 3: vram_size = AST_VIDMEM_SIZE_64M; break;
372         }
373
374         jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x99, 0xff);
375         switch (jreg & 0x03) {
376         case 1:
377                 vram_size -= 0x100000;
378                 break;
379         case 2:
380                 vram_size -= 0x200000;
381                 break;
382         case 3:
383                 vram_size -= 0x400000;
384                 break;
385         }
386
387         return vram_size;
388 }
389
390 int ast_driver_load(struct drm_device *dev, unsigned long flags)
391 {
392         struct ast_private *ast;
393         bool need_post;
394         int ret = 0;
395
396         ast = kzalloc(sizeof(struct ast_private), GFP_KERNEL);
397         if (!ast)
398                 return -ENOMEM;
399
400         dev->dev_private = ast;
401         ast->dev = dev;
402
403         ast->regs = pci_iomap(dev->pdev, 1, 0);
404         if (!ast->regs) {
405                 ret = -EIO;
406                 goto out_free;
407         }
408
409         /*
410          * If we don't have IO space at all, use MMIO now and
411          * assume the chip has MMIO enabled by default (rev 0x20
412          * and higher).
413          */
414         if (!(pci_resource_flags(dev->pdev, 2) & IORESOURCE_IO)) {
415                 DRM_INFO("platform has no IO space, trying MMIO\n");
416                 ast->ioregs = ast->regs + AST_IO_MM_OFFSET;
417         }
418
419         /* "map" IO regs if the above hasn't done so already */
420         if (!ast->ioregs) {
421                 ast->ioregs = pci_iomap(dev->pdev, 2, 0);
422                 if (!ast->ioregs) {
423                         ret = -EIO;
424                         goto out_free;
425                 }
426         }
427
428         ast_detect_chip(dev, &need_post);
429
430         if (ast->chip != AST1180) {
431                 ast_get_dram_info(dev);
432                 ast->vram_size = ast_get_vram_info(dev);
433                 DRM_INFO("dram %d %d %d %08x\n", ast->mclk, ast->dram_type, ast->dram_bus_width, ast->vram_size);
434         }
435
436         if (need_post)
437                 ast_post_gpu(dev);
438
439         ret = ast_mm_init(ast);
440         if (ret)
441                 goto out_free;
442
443         drm_mode_config_init(dev);
444
445         dev->mode_config.funcs = (void *)&ast_mode_funcs;
446         dev->mode_config.min_width = 0;
447         dev->mode_config.min_height = 0;
448         dev->mode_config.preferred_depth = 24;
449         dev->mode_config.prefer_shadow = 1;
450
451         if (ast->chip == AST2100 ||
452             ast->chip == AST2200 ||
453             ast->chip == AST2300 ||
454             ast->chip == AST2400 ||
455             ast->chip == AST1180) {
456                 dev->mode_config.max_width = 1920;
457                 dev->mode_config.max_height = 2048;
458         } else {
459                 dev->mode_config.max_width = 1600;
460                 dev->mode_config.max_height = 1200;
461         }
462
463         ret = ast_mode_init(dev);
464         if (ret)
465                 goto out_free;
466
467         ret = ast_fbdev_init(dev);
468         if (ret)
469                 goto out_free;
470
471         return 0;
472 out_free:
473         kfree(ast);
474         dev->dev_private = NULL;
475         return ret;
476 }
477
478 int ast_driver_unload(struct drm_device *dev)
479 {
480         struct ast_private *ast = dev->dev_private;
481
482         kfree(ast->dp501_fw_addr);
483         ast_mode_fini(dev);
484         ast_fbdev_fini(dev);
485         drm_mode_config_cleanup(dev);
486
487         ast_mm_fini(ast);
488         pci_iounmap(dev->pdev, ast->ioregs);
489         pci_iounmap(dev->pdev, ast->regs);
490         kfree(ast);
491         return 0;
492 }
493
494 int ast_gem_create(struct drm_device *dev,
495                    u32 size, bool iskernel,
496                    struct drm_gem_object **obj)
497 {
498         struct ast_bo *astbo;
499         int ret;
500
501         *obj = NULL;
502
503         size = roundup(size, PAGE_SIZE);
504         if (size == 0)
505                 return -EINVAL;
506
507         ret = ast_bo_create(dev, size, 0, 0, &astbo);
508         if (ret) {
509                 if (ret != -ERESTARTSYS)
510                         DRM_ERROR("failed to allocate GEM object\n");
511                 return ret;
512         }
513         *obj = &astbo->gem;
514         return 0;
515 }
516
517 int ast_dumb_create(struct drm_file *file,
518                     struct drm_device *dev,
519                     struct drm_mode_create_dumb *args)
520 {
521         int ret;
522         struct drm_gem_object *gobj;
523         u32 handle;
524
525         args->pitch = args->width * ((args->bpp + 7) / 8);
526         args->size = args->pitch * args->height;
527
528         ret = ast_gem_create(dev, args->size, false,
529                              &gobj);
530         if (ret)
531                 return ret;
532
533         ret = drm_gem_handle_create(file, gobj, &handle);
534         drm_gem_object_unreference_unlocked(gobj);
535         if (ret)
536                 return ret;
537
538         args->handle = handle;
539         return 0;
540 }
541
542 static void ast_bo_unref(struct ast_bo **bo)
543 {
544         struct ttm_buffer_object *tbo;
545
546         if ((*bo) == NULL)
547                 return;
548
549         tbo = &((*bo)->bo);
550         ttm_bo_unref(&tbo);
551         *bo = NULL;
552 }
553
554 void ast_gem_free_object(struct drm_gem_object *obj)
555 {
556         struct ast_bo *ast_bo = gem_to_ast_bo(obj);
557
558         ast_bo_unref(&ast_bo);
559 }
560
561
562 static inline u64 ast_bo_mmap_offset(struct ast_bo *bo)
563 {
564         return drm_vma_node_offset_addr(&bo->bo.vma_node);
565 }
566 int
567 ast_dumb_mmap_offset(struct drm_file *file,
568                      struct drm_device *dev,
569                      uint32_t handle,
570                      uint64_t *offset)
571 {
572         struct drm_gem_object *obj;
573         int ret;
574         struct ast_bo *bo;
575
576         mutex_lock(&dev->struct_mutex);
577         obj = drm_gem_object_lookup(dev, file, handle);
578         if (obj == NULL) {
579                 ret = -ENOENT;
580                 goto out_unlock;
581         }
582
583         bo = gem_to_ast_bo(obj);
584         *offset = ast_bo_mmap_offset(bo);
585
586         drm_gem_object_unreference(obj);
587         ret = 0;
588 out_unlock:
589         mutex_unlock(&dev->struct_mutex);
590         return ret;
591
592 }
593