blob: a2cc6be97983fe327cd4f67a4c3204cd4985691c [file] [log] [blame]
Dave Airlie312fec12012-02-29 13:40:04 +00001/*
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 */
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
Dave Airlie312fec12012-02-29 13:40:04 +000029#include "ast_drv.h"
30
31
David Howells760285e2012-10-02 18:01:07 +010032#include <drm/drm_fb_helper.h>
33#include <drm/drm_crtc_helper.h>
Dave Airlie312fec12012-02-29 13:40:04 +000034
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;
Dave Airlief1f62f22014-01-17 10:56:09 +100069 uint32_t data, jreg;
Dave Airlie312fec12012-02-29 13:40:04 +000070
71 if (dev->pdev->device == PCI_CHIP_AST1180) {
72 ast->chip = AST1100;
73 DRM_INFO("AST 1180 detected\n");
74 } else {
Dave Airlie1453bf42014-03-28 09:18:45 +100075 if (dev->pdev->revision >= 0x30) {
76 ast->chip = AST2400;
77 DRM_INFO("AST 2400 detected\n");
78 } else if (dev->pdev->revision >= 0x20) {
Dave Airlie312fec12012-02-29 13:40:04 +000079 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 }
Dave Airlief1f62f22014-01-17 10:56:09 +1000111
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;
Dave Airlie1453bf42014-03-28 09:18:45 +1000127 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;
Dave Airlief1f62f22014-01-17 10:56:09 +1000135 }
136 break;
137 }
138
Dave Airlie83c66202014-03-28 11:05:12 +1000139 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
Dave Airlie312fec12012-02-29 13:40:04 +0000164 return 0;
165}
166
167static 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
Dave Airlie1453bf42014-03-28 09:18:45 +1000189 if (ast->chip == AST2300 || ast->chip == AST2400) {
Dave Airlie312fec12012-02-29 13:40:04 +0000190 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
Dave Airlie312fec12012-02-29 13:40:04 +0000249static 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
Dave Airlie312fec12012-02-29 13:40:04 +0000259static const struct drm_framebuffer_funcs ast_fb_funcs = {
260 .destroy = ast_user_framebuffer_destroy,
Dave Airlie312fec12012-02-29 13:40:04 +0000261};
262
263
264int 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
Daniel Vetterc7d73f62012-12-13 23:38:38 +0100271 drm_helper_mode_fill_fb_struct(&ast_fb->base, mode_cmd);
272 ast_fb->obj = obj;
Dave Airlie312fec12012-02-29 13:40:04 +0000273 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 }
Dave Airlie312fec12012-02-29 13:40:04 +0000278 return 0;
279}
280
281static struct drm_framebuffer *
282ast_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
309static const struct drm_mode_config_funcs ast_mode_funcs = {
310 .fb_create = ast_user_framebuffer_create,
311};
312
313static u32 ast_get_vram_info(struct drm_device *dev)
314{
315 struct ast_private *ast = dev->dev_private;
316 u8 jreg;
Dave Airlie83c66202014-03-28 11:05:12 +1000317 u32 vram_size;
Dave Airlie312fec12012-02-29 13:40:04 +0000318 ast_open_key(ast);
319
Dave Airlie83c66202014-03-28 11:05:12 +1000320 vram_size = AST_VIDMEM_DEFAULT_SIZE;
Dave Airlie312fec12012-02-29 13:40:04 +0000321 jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xaa, 0xff);
322 switch (jreg & 3) {
Dave Airlie83c66202014-03-28 11:05:12 +1000323 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;
Dave Airlie312fec12012-02-29 13:40:04 +0000327 }
Dave Airlie83c66202014-03-28 11:05:12 +1000328
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;
Dave Airlie312fec12012-02-29 13:40:04 +0000343}
344
345int 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 ||
Dave Airlie1453bf42014-03-28 09:18:45 +1000391 ast->chip == AST2400 ||
Dave Airlie312fec12012-02-29 13:40:04 +0000392 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;
409out_free:
410 kfree(ast);
411 dev->dev_private = NULL;
412 return ret;
413}
414
415int ast_driver_unload(struct drm_device *dev)
416{
417 struct ast_private *ast = dev->dev_private;
418
Dave Airlie83c66202014-03-28 11:05:12 +1000419 kfree(ast->dp501_fw_addr);
Dave Airlie312fec12012-02-29 13:40:04 +0000420 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
431int 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
454int 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
Rashikaf6109802014-01-06 20:00:40 +0530479static void ast_bo_unref(struct ast_bo **bo)
Dave Airlie312fec12012-02-29 13:40:04 +0000480{
481 struct ttm_buffer_object *tbo;
482
483 if ((*bo) == NULL)
484 return;
485
486 tbo = &((*bo)->bo);
487 ttm_bo_unref(&tbo);
Daniel Vettereb649a62014-04-05 10:10:51 +0200488 *bo = NULL;
Dave Airlie312fec12012-02-29 13:40:04 +0000489}
Daniel Vettereb649a62014-04-05 10:10:51 +0200490
Dave Airlie312fec12012-02-29 13:40:04 +0000491void ast_gem_free_object(struct drm_gem_object *obj)
492{
493 struct ast_bo *ast_bo = gem_to_ast_bo(obj);
494
Dave Airlie312fec12012-02-29 13:40:04 +0000495 ast_bo_unref(&ast_bo);
496}
497
498
499static inline u64 ast_bo_mmap_offset(struct ast_bo *bo)
500{
David Herrmann72525b32013-07-24 21:08:53 +0200501 return drm_vma_node_offset_addr(&bo->bo.vma_node);
Dave Airlie312fec12012-02-29 13:40:04 +0000502}
503int
504ast_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;
525out_unlock:
526 mutex_unlock(&dev->struct_mutex);
527 return ret;
528
529}
530