Merge tag 'v3.10.107' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / gpu / drm / ast / ast_main.c
CommitLineData
312fec14
DA
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 */
760285e7 28#include <drm/drmP.h>
312fec14
DA
29#include "ast_drv.h"
30
31
760285e7
DH
32#include <drm/drm_fb_helper.h>
33#include <drm/drm_crtc_helper.h>
312fec14
DA
34
35#include "ast_dram_tables.h"
36
37void 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
47uint8_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
56uint8_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
66static int ast_detect_chip(struct drm_device *dev)
67{
68 struct ast_private *ast = dev->dev_private;
69
70 if (dev->pdev->device == PCI_CHIP_AST1180) {
71 ast->chip = AST1100;
72 DRM_INFO("AST 1180 detected\n");
73 } else {
74 if (dev->pdev->revision >= 0x20) {
75 ast->chip = AST2300;
76 DRM_INFO("AST 2300 detected\n");
77 } else if (dev->pdev->revision >= 0x10) {
78 uint32_t data;
79 ast_write32(ast, 0xf004, 0x1e6e0000);
80 ast_write32(ast, 0xf000, 0x1);
81
82 data = ast_read32(ast, 0x1207c);
83 switch (data & 0x0300) {
84 case 0x0200:
85 ast->chip = AST1100;
86 DRM_INFO("AST 1100 detected\n");
87 break;
88 case 0x0100:
89 ast->chip = AST2200;
90 DRM_INFO("AST 2200 detected\n");
91 break;
92 case 0x0000:
93 ast->chip = AST2150;
94 DRM_INFO("AST 2150 detected\n");
95 break;
96 default:
97 ast->chip = AST2100;
98 DRM_INFO("AST 2100 detected\n");
99 break;
100 }
101 ast->vga2_clone = false;
102 } else {
d287fccb 103 ast->chip = AST2000;
312fec14
DA
104 DRM_INFO("AST 2000 detected\n");
105 }
106 }
107 return 0;
108}
109
110static int ast_get_dram_info(struct drm_device *dev)
111{
112 struct ast_private *ast = dev->dev_private;
113 uint32_t data, data2;
114 uint32_t denum, num, div, ref_pll;
115
116 ast_write32(ast, 0xf004, 0x1e6e0000);
117 ast_write32(ast, 0xf000, 0x1);
118
119
120 ast_write32(ast, 0x10000, 0xfc600309);
121
122 do {
0175d923
RC
123 if (pci_channel_offline(dev->pdev))
124 return -EIO;
312fec14
DA
125 } while (ast_read32(ast, 0x10000) != 0x01);
126 data = ast_read32(ast, 0x10004);
127
eed92d81 128 if (data & 0x40)
312fec14
DA
129 ast->dram_bus_width = 16;
130 else
131 ast->dram_bus_width = 32;
132
133 if (ast->chip == AST2300) {
134 switch (data & 0x03) {
135 case 0:
136 ast->dram_type = AST_DRAM_512Mx16;
137 break;
138 default:
139 case 1:
140 ast->dram_type = AST_DRAM_1Gx16;
141 break;
142 case 2:
143 ast->dram_type = AST_DRAM_2Gx16;
144 break;
145 case 3:
146 ast->dram_type = AST_DRAM_4Gx16;
147 break;
148 }
149 } else {
150 switch (data & 0x0c) {
151 case 0:
152 case 4:
153 ast->dram_type = AST_DRAM_512Mx16;
154 break;
155 case 8:
156 if (data & 0x40)
157 ast->dram_type = AST_DRAM_1Gx16;
158 else
159 ast->dram_type = AST_DRAM_512Mx32;
160 break;
161 case 0xc:
162 ast->dram_type = AST_DRAM_1Gx32;
163 break;
164 }
165 }
166
167 data = ast_read32(ast, 0x10120);
168 data2 = ast_read32(ast, 0x10170);
169 if (data2 & 0x2000)
170 ref_pll = 14318;
171 else
172 ref_pll = 12000;
173
174 denum = data & 0x1f;
175 num = (data & 0x3fe0) >> 5;
176 data = (data & 0xc000) >> 14;
177 switch (data) {
178 case 3:
179 div = 0x4;
180 break;
181 case 2:
182 case 1:
183 div = 0x2;
184 break;
185 default:
186 div = 0x1;
187 break;
188 }
189 ast->mclk = ref_pll * (num + 2) / (denum + 2) * (div * 1000);
190 return 0;
191}
192
193uint32_t ast_get_max_dclk(struct drm_device *dev, int bpp)
194{
195 struct ast_private *ast = dev->dev_private;
196 uint32_t dclk, jreg;
197 uint32_t dram_bus_width, mclk, dram_bandwidth, actual_dram_bandwidth, dram_efficency = 500;
198
199 dram_bus_width = ast->dram_bus_width;
200 mclk = ast->mclk;
201
202 if (ast->chip == AST2100 ||
203 ast->chip == AST1100 ||
204 ast->chip == AST2200 ||
205 ast->chip == AST2150 ||
206 ast->dram_bus_width == 16)
207 dram_efficency = 600;
208 else if (ast->chip == AST2300)
209 dram_efficency = 400;
210
211 dram_bandwidth = mclk * dram_bus_width * 2 / 8;
212 actual_dram_bandwidth = dram_bandwidth * dram_efficency / 1000;
213
214 if (ast->chip == AST1180)
215 dclk = actual_dram_bandwidth / ((bpp + 1) / 8);
216 else {
217 jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd0, 0xff);
218 if ((jreg & 0x08) && (ast->chip == AST2000))
219 dclk = actual_dram_bandwidth / ((bpp + 1 + 16) / 8);
220 else if ((jreg & 0x08) && (bpp == 8))
221 dclk = actual_dram_bandwidth / ((bpp + 1 + 24) / 8);
222 else
223 dclk = actual_dram_bandwidth / ((bpp + 1) / 8);
224 }
225
226 if (ast->chip == AST2100 ||
227 ast->chip == AST2200 ||
228 ast->chip == AST2300 ||
229 ast->chip == AST1180) {
230 if (dclk > 200)
231 dclk = 200;
232 } else {
233 if (dclk > 165)
234 dclk = 165;
235 }
236
237 return dclk;
238}
239
240static void ast_user_framebuffer_destroy(struct drm_framebuffer *fb)
241{
242 struct ast_framebuffer *ast_fb = to_ast_framebuffer(fb);
243 if (ast_fb->obj)
244 drm_gem_object_unreference_unlocked(ast_fb->obj);
245
246 drm_framebuffer_cleanup(fb);
247 kfree(fb);
248}
249
312fec14
DA
250static const struct drm_framebuffer_funcs ast_fb_funcs = {
251 .destroy = ast_user_framebuffer_destroy,
312fec14
DA
252};
253
254
255int ast_framebuffer_init(struct drm_device *dev,
256 struct ast_framebuffer *ast_fb,
257 struct drm_mode_fb_cmd2 *mode_cmd,
258 struct drm_gem_object *obj)
259{
260 int ret;
261
c7d73f6a
DV
262 drm_helper_mode_fill_fb_struct(&ast_fb->base, mode_cmd);
263 ast_fb->obj = obj;
312fec14
DA
264 ret = drm_framebuffer_init(dev, &ast_fb->base, &ast_fb_funcs);
265 if (ret) {
266 DRM_ERROR("framebuffer init failed %d\n", ret);
267 return ret;
268 }
312fec14
DA
269 return 0;
270}
271
272static struct drm_framebuffer *
273ast_user_framebuffer_create(struct drm_device *dev,
274 struct drm_file *filp,
275 struct drm_mode_fb_cmd2 *mode_cmd)
276{
277 struct drm_gem_object *obj;
278 struct ast_framebuffer *ast_fb;
279 int ret;
280
281 obj = drm_gem_object_lookup(dev, filp, mode_cmd->handles[0]);
282 if (obj == NULL)
283 return ERR_PTR(-ENOENT);
284
285 ast_fb = kzalloc(sizeof(*ast_fb), GFP_KERNEL);
286 if (!ast_fb) {
287 drm_gem_object_unreference_unlocked(obj);
288 return ERR_PTR(-ENOMEM);
289 }
290
291 ret = ast_framebuffer_init(dev, ast_fb, mode_cmd, obj);
292 if (ret) {
293 drm_gem_object_unreference_unlocked(obj);
294 kfree(ast_fb);
295 return ERR_PTR(ret);
296 }
297 return &ast_fb->base;
298}
299
300static const struct drm_mode_config_funcs ast_mode_funcs = {
301 .fb_create = ast_user_framebuffer_create,
302};
303
304static u32 ast_get_vram_info(struct drm_device *dev)
305{
306 struct ast_private *ast = dev->dev_private;
307 u8 jreg;
308
309 ast_open_key(ast);
310
311 jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xaa, 0xff);
312 switch (jreg & 3) {
313 case 0: return AST_VIDMEM_SIZE_8M;
314 case 1: return AST_VIDMEM_SIZE_16M;
315 case 2: return AST_VIDMEM_SIZE_32M;
316 case 3: return AST_VIDMEM_SIZE_64M;
317 }
318 return AST_VIDMEM_DEFAULT_SIZE;
319}
320
321int ast_driver_load(struct drm_device *dev, unsigned long flags)
322{
323 struct ast_private *ast;
324 int ret = 0;
325
326 ast = kzalloc(sizeof(struct ast_private), GFP_KERNEL);
327 if (!ast)
328 return -ENOMEM;
329
330 dev->dev_private = ast;
331 ast->dev = dev;
332
333 ast->regs = pci_iomap(dev->pdev, 1, 0);
334 if (!ast->regs) {
335 ret = -EIO;
336 goto out_free;
337 }
338 ast->ioregs = pci_iomap(dev->pdev, 2, 0);
339 if (!ast->ioregs) {
340 ret = -EIO;
341 goto out_free;
342 }
343
344 ast_detect_chip(dev);
345
346 if (ast->chip != AST1180) {
0175d923
RC
347 ret = ast_get_dram_info(dev);
348 if (ret)
349 goto out_free;
312fec14
DA
350 ast->vram_size = ast_get_vram_info(dev);
351 DRM_INFO("dram %d %d %d %08x\n", ast->mclk, ast->dram_type, ast->dram_bus_width, ast->vram_size);
352 }
353
354 ret = ast_mm_init(ast);
355 if (ret)
356 goto out_free;
357
358 drm_mode_config_init(dev);
359
360 dev->mode_config.funcs = (void *)&ast_mode_funcs;
361 dev->mode_config.min_width = 0;
362 dev->mode_config.min_height = 0;
363 dev->mode_config.preferred_depth = 24;
364 dev->mode_config.prefer_shadow = 1;
9f5bc010 365 dev->mode_config.fb_base = pci_resource_start(ast->dev->pdev, 0);
312fec14
DA
366
367 if (ast->chip == AST2100 ||
368 ast->chip == AST2200 ||
369 ast->chip == AST2300 ||
370 ast->chip == AST1180) {
371 dev->mode_config.max_width = 1920;
372 dev->mode_config.max_height = 2048;
373 } else {
374 dev->mode_config.max_width = 1600;
375 dev->mode_config.max_height = 1200;
376 }
377
378 ret = ast_mode_init(dev);
379 if (ret)
380 goto out_free;
381
382 ret = ast_fbdev_init(dev);
383 if (ret)
384 goto out_free;
385
386 return 0;
387out_free:
388 kfree(ast);
389 dev->dev_private = NULL;
390 return ret;
391}
392
393int ast_driver_unload(struct drm_device *dev)
394{
395 struct ast_private *ast = dev->dev_private;
396
397 ast_mode_fini(dev);
398 ast_fbdev_fini(dev);
399 drm_mode_config_cleanup(dev);
400
401 ast_mm_fini(ast);
402 pci_iounmap(dev->pdev, ast->ioregs);
403 pci_iounmap(dev->pdev, ast->regs);
404 kfree(ast);
405 return 0;
406}
407
408int ast_gem_create(struct drm_device *dev,
409 u32 size, bool iskernel,
410 struct drm_gem_object **obj)
411{
412 struct ast_bo *astbo;
413 int ret;
414
415 *obj = NULL;
416
417 size = roundup(size, PAGE_SIZE);
418 if (size == 0)
419 return -EINVAL;
420
421 ret = ast_bo_create(dev, size, 0, 0, &astbo);
422 if (ret) {
423 if (ret != -ERESTARTSYS)
424 DRM_ERROR("failed to allocate GEM object\n");
425 return ret;
426 }
427 *obj = &astbo->gem;
428 return 0;
429}
430
431int ast_dumb_create(struct drm_file *file,
432 struct drm_device *dev,
433 struct drm_mode_create_dumb *args)
434{
435 int ret;
436 struct drm_gem_object *gobj;
437 u32 handle;
438
439 args->pitch = args->width * ((args->bpp + 7) / 8);
440 args->size = args->pitch * args->height;
441
442 ret = ast_gem_create(dev, args->size, false,
443 &gobj);
444 if (ret)
445 return ret;
446
447 ret = drm_gem_handle_create(file, gobj, &handle);
448 drm_gem_object_unreference_unlocked(gobj);
449 if (ret)
450 return ret;
451
452 args->handle = handle;
453 return 0;
454}
455
456int ast_dumb_destroy(struct drm_file *file,
457 struct drm_device *dev,
458 uint32_t handle)
459{
460 return drm_gem_handle_delete(file, handle);
461}
462
463int ast_gem_init_object(struct drm_gem_object *obj)
464{
465 BUG();
466 return 0;
467}
468
469void ast_bo_unref(struct ast_bo **bo)
470{
471 struct ttm_buffer_object *tbo;
472
473 if ((*bo) == NULL)
474 return;
475
476 tbo = &((*bo)->bo);
477 ttm_bo_unref(&tbo);
478 if (tbo == NULL)
479 *bo = NULL;
480
481}
482void ast_gem_free_object(struct drm_gem_object *obj)
483{
484 struct ast_bo *ast_bo = gem_to_ast_bo(obj);
485
486 if (!ast_bo)
487 return;
488 ast_bo_unref(&ast_bo);
489}
490
491
492static inline u64 ast_bo_mmap_offset(struct ast_bo *bo)
493{
494 return bo->bo.addr_space_offset;
495}
496int
497ast_dumb_mmap_offset(struct drm_file *file,
498 struct drm_device *dev,
499 uint32_t handle,
500 uint64_t *offset)
501{
502 struct drm_gem_object *obj;
503 int ret;
504 struct ast_bo *bo;
505
506 mutex_lock(&dev->struct_mutex);
507 obj = drm_gem_object_lookup(dev, file, handle);
508 if (obj == NULL) {
509 ret = -ENOENT;
510 goto out_unlock;
511 }
512
513 bo = gem_to_ast_bo(obj);
514 *offset = ast_bo_mmap_offset(bo);
515
516 drm_gem_object_unreference(obj);
517 ret = 0;
518out_unlock:
519 mutex_unlock(&dev->struct_mutex);
520 return ret;
521
522}
523