Merge tag 'for-linus-20140905' of git://git.infradead.org/linux-mtd
[firefly-linux-kernel-4.4.55.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)
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         switch (ast->chip) {
113         case AST1180:
114                 ast->support_wide_screen = true;
115                 break;
116         case AST2000:
117                 ast->support_wide_screen = false;
118                 break;
119         default:
120                 jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd0, 0xff);
121                 if (!(jreg & 0x80))
122                         ast->support_wide_screen = true;
123                 else if (jreg & 0x01)
124                         ast->support_wide_screen = true;
125                 else {
126                         ast->support_wide_screen = false;
127                         ast_write32(ast, 0xf004, 0x1e6e0000);
128                         ast_write32(ast, 0xf000, 0x1);
129                         data = ast_read32(ast, 0x1207c);
130                         data &= 0x300;
131                         if (ast->chip == AST2300 && data == 0x0) /* ast1300 */
132                                 ast->support_wide_screen = true;
133                         if (ast->chip == AST2400 && data == 0x100) /* ast1400 */
134                                 ast->support_wide_screen = true;
135                 }
136                 break;
137         }
138
139         ast->tx_chip_type = AST_TX_NONE;
140         jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa3, 0xff);
141         if (jreg & 0x80)
142                 ast->tx_chip_type = AST_TX_SIL164;
143         if ((ast->chip == AST2300) || (ast->chip == AST2400)) {
144                 jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd1, 0xff);
145                 switch (jreg) {
146                 case 0x04:
147                         ast->tx_chip_type = AST_TX_SIL164;
148                         break;
149                 case 0x08:
150                         ast->dp501_fw_addr = kzalloc(32*1024, GFP_KERNEL);
151                         if (ast->dp501_fw_addr) {
152                                 /* backup firmware */
153                                 if (ast_backup_fw(dev, ast->dp501_fw_addr, 32*1024)) {
154                                         kfree(ast->dp501_fw_addr);
155                                         ast->dp501_fw_addr = NULL;
156                                 }
157                         }
158                         /* fallthrough */
159                 case 0x0c:
160                         ast->tx_chip_type = AST_TX_DP501;
161                 }
162         }
163
164         return 0;
165 }
166
167 static int ast_get_dram_info(struct drm_device *dev)
168 {
169         struct ast_private *ast = dev->dev_private;
170         uint32_t data, data2;
171         uint32_t denum, num, div, ref_pll;
172
173         ast_write32(ast, 0xf004, 0x1e6e0000);
174         ast_write32(ast, 0xf000, 0x1);
175
176
177         ast_write32(ast, 0x10000, 0xfc600309);
178
179         do {
180                 ;
181         } while (ast_read32(ast, 0x10000) != 0x01);
182         data = ast_read32(ast, 0x10004);
183
184         if (data & 0x400)
185                 ast->dram_bus_width = 16;
186         else
187                 ast->dram_bus_width = 32;
188
189         if (ast->chip == AST2300 || ast->chip == AST2400) {
190                 switch (data & 0x03) {
191                 case 0:
192                         ast->dram_type = AST_DRAM_512Mx16;
193                         break;
194                 default:
195                 case 1:
196                         ast->dram_type = AST_DRAM_1Gx16;
197                         break;
198                 case 2:
199                         ast->dram_type = AST_DRAM_2Gx16;
200                         break;
201                 case 3:
202                         ast->dram_type = AST_DRAM_4Gx16;
203                         break;
204                 }
205         } else {
206                 switch (data & 0x0c) {
207                 case 0:
208                 case 4:
209                         ast->dram_type = AST_DRAM_512Mx16;
210                         break;
211                 case 8:
212                         if (data & 0x40)
213                                 ast->dram_type = AST_DRAM_1Gx16;
214                         else
215                                 ast->dram_type = AST_DRAM_512Mx32;
216                         break;
217                 case 0xc:
218                         ast->dram_type = AST_DRAM_1Gx32;
219                         break;
220                 }
221         }
222
223         data = ast_read32(ast, 0x10120);
224         data2 = ast_read32(ast, 0x10170);
225         if (data2 & 0x2000)
226                 ref_pll = 14318;
227         else
228                 ref_pll = 12000;
229
230         denum = data & 0x1f;
231         num = (data & 0x3fe0) >> 5;
232         data = (data & 0xc000) >> 14;
233         switch (data) {
234         case 3:
235                 div = 0x4;
236                 break;
237         case 2:
238         case 1:
239                 div = 0x2;
240                 break;
241         default:
242                 div = 0x1;
243                 break;
244         }
245         ast->mclk = ref_pll * (num + 2) / (denum + 2) * (div * 1000);
246         return 0;
247 }
248
249 static void ast_user_framebuffer_destroy(struct drm_framebuffer *fb)
250 {
251         struct ast_framebuffer *ast_fb = to_ast_framebuffer(fb);
252         if (ast_fb->obj)
253                 drm_gem_object_unreference_unlocked(ast_fb->obj);
254
255         drm_framebuffer_cleanup(fb);
256         kfree(fb);
257 }
258
259 static const struct drm_framebuffer_funcs ast_fb_funcs = {
260         .destroy = ast_user_framebuffer_destroy,
261 };
262
263
264 int ast_framebuffer_init(struct drm_device *dev,
265                          struct ast_framebuffer *ast_fb,
266                          struct drm_mode_fb_cmd2 *mode_cmd,
267                          struct drm_gem_object *obj)
268 {
269         int ret;
270
271         drm_helper_mode_fill_fb_struct(&ast_fb->base, mode_cmd);
272         ast_fb->obj = obj;
273         ret = drm_framebuffer_init(dev, &ast_fb->base, &ast_fb_funcs);
274         if (ret) {
275                 DRM_ERROR("framebuffer init failed %d\n", ret);
276                 return ret;
277         }
278         return 0;
279 }
280
281 static struct drm_framebuffer *
282 ast_user_framebuffer_create(struct drm_device *dev,
283                struct drm_file *filp,
284                struct drm_mode_fb_cmd2 *mode_cmd)
285 {
286         struct drm_gem_object *obj;
287         struct ast_framebuffer *ast_fb;
288         int ret;
289
290         obj = drm_gem_object_lookup(dev, filp, mode_cmd->handles[0]);
291         if (obj == NULL)
292                 return ERR_PTR(-ENOENT);
293
294         ast_fb = kzalloc(sizeof(*ast_fb), GFP_KERNEL);
295         if (!ast_fb) {
296                 drm_gem_object_unreference_unlocked(obj);
297                 return ERR_PTR(-ENOMEM);
298         }
299
300         ret = ast_framebuffer_init(dev, ast_fb, mode_cmd, obj);
301         if (ret) {
302                 drm_gem_object_unreference_unlocked(obj);
303                 kfree(ast_fb);
304                 return ERR_PTR(ret);
305         }
306         return &ast_fb->base;
307 }
308
309 static const struct drm_mode_config_funcs ast_mode_funcs = {
310         .fb_create = ast_user_framebuffer_create,
311 };
312
313 static u32 ast_get_vram_info(struct drm_device *dev)
314 {
315         struct ast_private *ast = dev->dev_private;
316         u8 jreg;
317         u32 vram_size;
318         ast_open_key(ast);
319
320         vram_size = AST_VIDMEM_DEFAULT_SIZE;
321         jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xaa, 0xff);
322         switch (jreg & 3) {
323         case 0: vram_size = AST_VIDMEM_SIZE_8M; break;
324         case 1: vram_size = AST_VIDMEM_SIZE_16M; break;
325         case 2: vram_size = AST_VIDMEM_SIZE_32M; break;
326         case 3: vram_size = AST_VIDMEM_SIZE_64M; break;
327         }
328
329         jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x99, 0xff);
330         switch (jreg & 0x03) {
331         case 1:
332                 vram_size -= 0x100000;
333                 break;
334         case 2:
335                 vram_size -= 0x200000;
336                 break;
337         case 3:
338                 vram_size -= 0x400000;
339                 break;
340         }
341
342         return vram_size;
343 }
344
345 int ast_driver_load(struct drm_device *dev, unsigned long flags)
346 {
347         struct ast_private *ast;
348         int ret = 0;
349
350         ast = kzalloc(sizeof(struct ast_private), GFP_KERNEL);
351         if (!ast)
352                 return -ENOMEM;
353
354         dev->dev_private = ast;
355         ast->dev = dev;
356
357         ast->regs = pci_iomap(dev->pdev, 1, 0);
358         if (!ast->regs) {
359                 ret = -EIO;
360                 goto out_free;
361         }
362         ast->ioregs = pci_iomap(dev->pdev, 2, 0);
363         if (!ast->ioregs) {
364                 ret = -EIO;
365                 goto out_free;
366         }
367
368         ast_detect_chip(dev);
369
370         if (ast->chip != AST1180) {
371                 ast_get_dram_info(dev);
372                 ast->vram_size = ast_get_vram_info(dev);
373                 DRM_INFO("dram %d %d %d %08x\n", ast->mclk, ast->dram_type, ast->dram_bus_width, ast->vram_size);
374         }
375
376         ret = ast_mm_init(ast);
377         if (ret)
378                 goto out_free;
379
380         drm_mode_config_init(dev);
381
382         dev->mode_config.funcs = (void *)&ast_mode_funcs;
383         dev->mode_config.min_width = 0;
384         dev->mode_config.min_height = 0;
385         dev->mode_config.preferred_depth = 24;
386         dev->mode_config.prefer_shadow = 1;
387
388         if (ast->chip == AST2100 ||
389             ast->chip == AST2200 ||
390             ast->chip == AST2300 ||
391             ast->chip == AST2400 ||
392             ast->chip == AST1180) {
393                 dev->mode_config.max_width = 1920;
394                 dev->mode_config.max_height = 2048;
395         } else {
396                 dev->mode_config.max_width = 1600;
397                 dev->mode_config.max_height = 1200;
398         }
399
400         ret = ast_mode_init(dev);
401         if (ret)
402                 goto out_free;
403
404         ret = ast_fbdev_init(dev);
405         if (ret)
406                 goto out_free;
407
408         return 0;
409 out_free:
410         kfree(ast);
411         dev->dev_private = NULL;
412         return ret;
413 }
414
415 int ast_driver_unload(struct drm_device *dev)
416 {
417         struct ast_private *ast = dev->dev_private;
418
419         kfree(ast->dp501_fw_addr);
420         ast_mode_fini(dev);
421         ast_fbdev_fini(dev);
422         drm_mode_config_cleanup(dev);
423
424         ast_mm_fini(ast);
425         pci_iounmap(dev->pdev, ast->ioregs);
426         pci_iounmap(dev->pdev, ast->regs);
427         kfree(ast);
428         return 0;
429 }
430
431 int ast_gem_create(struct drm_device *dev,
432                    u32 size, bool iskernel,
433                    struct drm_gem_object **obj)
434 {
435         struct ast_bo *astbo;
436         int ret;
437
438         *obj = NULL;
439
440         size = roundup(size, PAGE_SIZE);
441         if (size == 0)
442                 return -EINVAL;
443
444         ret = ast_bo_create(dev, size, 0, 0, &astbo);
445         if (ret) {
446                 if (ret != -ERESTARTSYS)
447                         DRM_ERROR("failed to allocate GEM object\n");
448                 return ret;
449         }
450         *obj = &astbo->gem;
451         return 0;
452 }
453
454 int ast_dumb_create(struct drm_file *file,
455                     struct drm_device *dev,
456                     struct drm_mode_create_dumb *args)
457 {
458         int ret;
459         struct drm_gem_object *gobj;
460         u32 handle;
461
462         args->pitch = args->width * ((args->bpp + 7) / 8);
463         args->size = args->pitch * args->height;
464
465         ret = ast_gem_create(dev, args->size, false,
466                              &gobj);
467         if (ret)
468                 return ret;
469
470         ret = drm_gem_handle_create(file, gobj, &handle);
471         drm_gem_object_unreference_unlocked(gobj);
472         if (ret)
473                 return ret;
474
475         args->handle = handle;
476         return 0;
477 }
478
479 static void ast_bo_unref(struct ast_bo **bo)
480 {
481         struct ttm_buffer_object *tbo;
482
483         if ((*bo) == NULL)
484                 return;
485
486         tbo = &((*bo)->bo);
487         ttm_bo_unref(&tbo);
488         *bo = NULL;
489 }
490
491 void ast_gem_free_object(struct drm_gem_object *obj)
492 {
493         struct ast_bo *ast_bo = gem_to_ast_bo(obj);
494
495         ast_bo_unref(&ast_bo);
496 }
497
498
499 static inline u64 ast_bo_mmap_offset(struct ast_bo *bo)
500 {
501         return drm_vma_node_offset_addr(&bo->bo.vma_node);
502 }
503 int
504 ast_dumb_mmap_offset(struct drm_file *file,
505                      struct drm_device *dev,
506                      uint32_t handle,
507                      uint64_t *offset)
508 {
509         struct drm_gem_object *obj;
510         int ret;
511         struct ast_bo *bo;
512
513         mutex_lock(&dev->struct_mutex);
514         obj = drm_gem_object_lookup(dev, file, handle);
515         if (obj == NULL) {
516                 ret = -ENOENT;
517                 goto out_unlock;
518         }
519
520         bo = gem_to_ast_bo(obj);
521         *offset = ast_bo_mmap_offset(bo);
522
523         drm_gem_object_unreference(obj);
524         ret = 0;
525 out_unlock:
526         mutex_unlock(&dev->struct_mutex);
527         return ret;
528
529 }
530