drm/ast: Try to use MMIO registers when PIO isn't supported
[deliverable/linux.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;
f1f62f2c 69 uint32_t data, jreg;
312fec14
DA
70
71 if (dev->pdev->device == PCI_CHIP_AST1180) {
72 ast->chip = AST1100;
73 DRM_INFO("AST 1180 detected\n");
74 } else {
1453bf4c
DA
75 if (dev->pdev->revision >= 0x30) {
76 ast->chip = AST2400;
77 DRM_INFO("AST 2400 detected\n");
78 } else if (dev->pdev->revision >= 0x20) {
312fec14
DA
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 }
f1f62f2c
DA
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;
1453bf4c
DA
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;
f1f62f2c
DA
135 }
136 break;
137 }
138
83c6620b
DA
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
312fec14
DA
164 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
1453bf4c 189 if (ast->chip == AST2300 || ast->chip == AST2400) {
312fec14
DA
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
312fec14
DA
249static 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
312fec14
DA
259static const struct drm_framebuffer_funcs ast_fb_funcs = {
260 .destroy = ast_user_framebuffer_destroy,
312fec14
DA
261};
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
c7d73f6a
DV
271 drm_helper_mode_fill_fb_struct(&ast_fb->base, mode_cmd);
272 ast_fb->obj = obj;
312fec14
DA
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 }
312fec14
DA
278 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;
83c6620b 317 u32 vram_size;
312fec14
DA
318 ast_open_key(ast);
319
83c6620b 320 vram_size = AST_VIDMEM_DEFAULT_SIZE;
312fec14
DA
321 jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xaa, 0xff);
322 switch (jreg & 3) {
83c6620b
DA
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;
312fec14 327 }
83c6620b
DA
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;
312fec14
DA
343}
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 }
0dd68309
BH
362
363 /*
364 * If we don't have IO space at all, use MMIO now and
365 * assume the chip has MMIO enabled by default (rev 0x20
366 * and higher).
367 */
368 if (!(pci_resource_flags(dev->pdev, 2) & IORESOURCE_IO)) {
369 DRM_INFO("platform has no IO space, trying MMIO\n");
370 ast->ioregs = ast->regs + AST_IO_MM_OFFSET;
371 }
372
373 /* "map" IO regs if the above hasn't done so already */
312fec14 374 if (!ast->ioregs) {
0dd68309
BH
375 ast->ioregs = pci_iomap(dev->pdev, 2, 0);
376 if (!ast->ioregs) {
377 ret = -EIO;
378 goto out_free;
379 }
312fec14
DA
380 }
381
382 ast_detect_chip(dev);
383
384 if (ast->chip != AST1180) {
385 ast_get_dram_info(dev);
386 ast->vram_size = ast_get_vram_info(dev);
387 DRM_INFO("dram %d %d %d %08x\n", ast->mclk, ast->dram_type, ast->dram_bus_width, ast->vram_size);
388 }
389
390 ret = ast_mm_init(ast);
391 if (ret)
392 goto out_free;
393
394 drm_mode_config_init(dev);
395
396 dev->mode_config.funcs = (void *)&ast_mode_funcs;
397 dev->mode_config.min_width = 0;
398 dev->mode_config.min_height = 0;
399 dev->mode_config.preferred_depth = 24;
400 dev->mode_config.prefer_shadow = 1;
401
402 if (ast->chip == AST2100 ||
403 ast->chip == AST2200 ||
404 ast->chip == AST2300 ||
1453bf4c 405 ast->chip == AST2400 ||
312fec14
DA
406 ast->chip == AST1180) {
407 dev->mode_config.max_width = 1920;
408 dev->mode_config.max_height = 2048;
409 } else {
410 dev->mode_config.max_width = 1600;
411 dev->mode_config.max_height = 1200;
412 }
413
414 ret = ast_mode_init(dev);
415 if (ret)
416 goto out_free;
417
418 ret = ast_fbdev_init(dev);
419 if (ret)
420 goto out_free;
421
422 return 0;
423out_free:
424 kfree(ast);
425 dev->dev_private = NULL;
426 return ret;
427}
428
429int ast_driver_unload(struct drm_device *dev)
430{
431 struct ast_private *ast = dev->dev_private;
432
83c6620b 433 kfree(ast->dp501_fw_addr);
312fec14
DA
434 ast_mode_fini(dev);
435 ast_fbdev_fini(dev);
436 drm_mode_config_cleanup(dev);
437
438 ast_mm_fini(ast);
439 pci_iounmap(dev->pdev, ast->ioregs);
440 pci_iounmap(dev->pdev, ast->regs);
441 kfree(ast);
442 return 0;
443}
444
445int ast_gem_create(struct drm_device *dev,
446 u32 size, bool iskernel,
447 struct drm_gem_object **obj)
448{
449 struct ast_bo *astbo;
450 int ret;
451
452 *obj = NULL;
453
454 size = roundup(size, PAGE_SIZE);
455 if (size == 0)
456 return -EINVAL;
457
458 ret = ast_bo_create(dev, size, 0, 0, &astbo);
459 if (ret) {
460 if (ret != -ERESTARTSYS)
461 DRM_ERROR("failed to allocate GEM object\n");
462 return ret;
463 }
464 *obj = &astbo->gem;
465 return 0;
466}
467
468int ast_dumb_create(struct drm_file *file,
469 struct drm_device *dev,
470 struct drm_mode_create_dumb *args)
471{
472 int ret;
473 struct drm_gem_object *gobj;
474 u32 handle;
475
476 args->pitch = args->width * ((args->bpp + 7) / 8);
477 args->size = args->pitch * args->height;
478
479 ret = ast_gem_create(dev, args->size, false,
480 &gobj);
481 if (ret)
482 return ret;
483
484 ret = drm_gem_handle_create(file, gobj, &handle);
485 drm_gem_object_unreference_unlocked(gobj);
486 if (ret)
487 return ret;
488
489 args->handle = handle;
490 return 0;
491}
492
f6109803 493static void ast_bo_unref(struct ast_bo **bo)
312fec14
DA
494{
495 struct ttm_buffer_object *tbo;
496
497 if ((*bo) == NULL)
498 return;
499
500 tbo = &((*bo)->bo);
501 ttm_bo_unref(&tbo);
eb649a61 502 *bo = NULL;
312fec14 503}
eb649a61 504
312fec14
DA
505void ast_gem_free_object(struct drm_gem_object *obj)
506{
507 struct ast_bo *ast_bo = gem_to_ast_bo(obj);
508
312fec14
DA
509 ast_bo_unref(&ast_bo);
510}
511
512
513static inline u64 ast_bo_mmap_offset(struct ast_bo *bo)
514{
72525b3f 515 return drm_vma_node_offset_addr(&bo->bo.vma_node);
312fec14
DA
516}
517int
518ast_dumb_mmap_offset(struct drm_file *file,
519 struct drm_device *dev,
520 uint32_t handle,
521 uint64_t *offset)
522{
523 struct drm_gem_object *obj;
524 int ret;
525 struct ast_bo *bo;
526
527 mutex_lock(&dev->struct_mutex);
528 obj = drm_gem_object_lookup(dev, file, handle);
529 if (obj == NULL) {
530 ret = -ENOENT;
531 goto out_unlock;
532 }
533
534 bo = gem_to_ast_bo(obj);
535 *offset = ast_bo_mmap_offset(bo);
536
537 drm_gem_object_unreference(obj);
538 ret = 0;
539out_unlock:
540 mutex_unlock(&dev->struct_mutex);
541 return ret;
542
543}
544
This page took 0.194486 seconds and 5 git commands to generate.