Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
35static 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
78static void mgag200_mm_release(struct drm_device *dev, void *ptr)
79{
80 struct mga_device *mdev = to_mga_device(dev);
81
82 mdev->vram_fb_available = 0;
83 iounmap(mdev->vram);
84 arch_io_free_memtype_wc(pci_resource_start(dev->pdev, 0),
85 pci_resource_len(dev->pdev, 0));
86 arch_phys_wc_del(mdev->fb_mtrr);
87 mdev->fb_mtrr = 0;
88}
89
90int mgag200_mm_init(struct mga_device *mdev)
91{
92 struct drm_device *dev = &mdev->base;
93 resource_size_t start, len;
94 int ret;
95
96 /* BAR 0 is VRAM */
97 start = pci_resource_start(dev->pdev, 0);
98 len = pci_resource_len(dev->pdev, 0);
99
100 if (!devm_request_mem_region(dev->dev, start, len, "mgadrmfb_vram")) {
101 drm_err(dev, "can't reserve VRAM\n");
102 return -ENXIO;
103 }
104
105 arch_io_reserve_memtype_wc(start, len);
106
107 mdev->fb_mtrr = arch_phys_wc_add(start, len);
108
109 mdev->vram = ioremap(start, len);
110 if (!mdev->vram) {
111 ret = -ENOMEM;
112 goto err_arch_phys_wc_del;
113 }
114
115 mdev->mc.vram_size = mgag200_probe_vram(mdev, mdev->vram, len);
116 mdev->mc.vram_base = start;
117 mdev->mc.vram_window = len;
118
119 mdev->vram_fb_available = mdev->mc.vram_size;
120
121 return drmm_add_action_or_reset(dev, mgag200_mm_release, NULL);
122
123err_arch_phys_wc_del:
124 arch_phys_wc_del(mdev->fb_mtrr);
125 arch_io_free_memtype_wc(start, len);
126 return ret;
127}