• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
33 #include "mgag200_drv.h"
34 
mgag200_probe_vram(struct mga_device * mdev,void __iomem * mem,size_t size)35 static size_t mgag200_probe_vram(struct mga_device *mdev, void __iomem *mem,
36 				 size_t size)
37 {
38 	int offset;
39 	int orig;
40 	int test1, test2;
41 	int orig1, orig2;
42 	size_t vram_size;
43 
44 	/* Probe */
45 	orig = ioread16(mem);
46 	iowrite16(0, mem);
47 
48 	vram_size = size;
49 
50 	if ((mdev->type == G200_EW3) && (vram_size >= 0x1000000))
51 		vram_size = vram_size - 0x400000;
52 
53 	for (offset = 0x100000; offset < vram_size; offset += 0x4000) {
54 		orig1 = ioread8(mem + offset);
55 		orig2 = ioread8(mem + offset + 0x100);
56 
57 		iowrite16(0xaa55, mem + offset);
58 		iowrite16(0xaa55, mem + offset + 0x100);
59 
60 		test1 = ioread16(mem + offset);
61 		test2 = ioread16(mem);
62 
63 		iowrite16(orig1, mem + offset);
64 		iowrite16(orig2, mem + offset + 0x100);
65 
66 		if (test1 != 0xaa55)
67 			break;
68 
69 		if (test2)
70 			break;
71 	}
72 
73 	iowrite16(orig, mem);
74 
75 	return offset - 65536;
76 }
77 
mgag200_mm_release(struct drm_device * dev,void * ptr)78 static void mgag200_mm_release(struct drm_device *dev, void *ptr)
79 {
80 	struct mga_device *mdev = to_mga_device(dev);
81 	struct pci_dev *pdev = to_pci_dev(dev->dev);
82 
83 	mdev->vram_fb_available = 0;
84 	iounmap(mdev->vram);
85 	arch_io_free_memtype_wc(pci_resource_start(pdev, 0),
86 				pci_resource_len(pdev, 0));
87 	arch_phys_wc_del(mdev->fb_mtrr);
88 	mdev->fb_mtrr = 0;
89 }
90 
mgag200_mm_init(struct mga_device * mdev)91 int mgag200_mm_init(struct mga_device *mdev)
92 {
93 	struct drm_device *dev = &mdev->base;
94 	struct pci_dev *pdev = to_pci_dev(dev->dev);
95 	u8 misc;
96 	resource_size_t start, len;
97 	int ret;
98 
99 	WREG_ECRT(0x04, 0x00);
100 
101 	misc = RREG8(MGA_MISC_IN);
102 	misc |= MGAREG_MISC_RAMMAPEN |
103 		MGAREG_MISC_HIGH_PG_SEL;
104 	WREG8(MGA_MISC_OUT, misc);
105 
106 	/* BAR 0 is VRAM */
107 	start = pci_resource_start(pdev, 0);
108 	len = pci_resource_len(pdev, 0);
109 
110 	if (!devm_request_mem_region(dev->dev, start, len, "mgadrmfb_vram")) {
111 		drm_err(dev, "can't reserve VRAM\n");
112 		return -ENXIO;
113 	}
114 
115 	arch_io_reserve_memtype_wc(start, len);
116 
117 	mdev->fb_mtrr = arch_phys_wc_add(start, len);
118 
119 	mdev->vram = ioremap(start, len);
120 	if (!mdev->vram) {
121 		ret = -ENOMEM;
122 		goto err_arch_phys_wc_del;
123 	}
124 
125 	mdev->mc.vram_size = mgag200_probe_vram(mdev, mdev->vram, len);
126 	mdev->mc.vram_base = start;
127 	mdev->mc.vram_window = len;
128 
129 	mdev->vram_fb_available = mdev->mc.vram_size;
130 
131 	return drmm_add_action_or_reset(dev, mgag200_mm_release, NULL);
132 
133 err_arch_phys_wc_del:
134 	arch_phys_wc_del(mdev->fb_mtrr);
135 	arch_io_free_memtype_wc(start, len);
136 	return ret;
137 }
138