xref: /linux/drivers/gpu/drm/ast/ast_mm.c (revision fcab107abe1ab5be9dbe874baa722372da8f4f73)
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 
29 #include <linux/pci.h>
30 
31 #include <drm/drm_managed.h>
32 #include <drm/drm_print.h>
33 
34 #include "ast_drv.h"
35 
36 static u32 ast_get_vram_size(struct ast_device *ast)
37 {
38 	u32 vram_size;
39 	u8 vgacr99, vgacraa;
40 
41 	vgacraa = ast_get_index_reg(ast, AST_IO_VGACRI, 0xaa);
42 	switch (vgacraa & AST_IO_VGACRAA_VGAMEM_SIZE_MASK) {
43 	case 0:
44 		vram_size = SZ_8M;
45 		break;
46 	case 1:
47 		vram_size = SZ_16M;
48 		break;
49 	case 2:
50 		vram_size = SZ_32M;
51 		break;
52 	case 3:
53 		vram_size = SZ_64M;
54 		break;
55 	}
56 
57 	vgacr99 = ast_get_index_reg(ast, AST_IO_VGACRI, 0x99);
58 	switch (vgacr99 & AST_IO_VGACR99_VGAMEM_RSRV_MASK) {
59 	case 1:
60 		vram_size -= SZ_1M;
61 		break;
62 	case 2:
63 		vram_size -= SZ_2M;
64 		break;
65 	case 3:
66 		vram_size -= SZ_4M;
67 		break;
68 	}
69 
70 	return vram_size;
71 }
72 
73 int ast_mm_init(struct ast_device *ast)
74 {
75 	struct drm_device *dev = &ast->base;
76 	struct pci_dev *pdev = to_pci_dev(dev->dev);
77 	resource_size_t base, size;
78 	u32 vram_size;
79 
80 	base = pci_resource_start(pdev, 0);
81 	size = pci_resource_len(pdev, 0);
82 
83 	/* Don't fail on errors, but performance might be reduced. */
84 	devm_arch_io_reserve_memtype_wc(dev->dev, base, size);
85 	devm_arch_phys_wc_add(dev->dev, base, size);
86 
87 	vram_size = ast_get_vram_size(ast);
88 
89 	ast->vram = devm_ioremap_wc(dev->dev, base, vram_size);
90 	if (!ast->vram)
91 		return -ENOMEM;
92 
93 	ast->vram_base = base;
94 	ast->vram_size = vram_size;
95 
96 	return 0;
97 }
98