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 "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Ben Skeggs
23  */
24 #include "gf100.h"
25 #include "ram.h"
26 
27 #include <core/memory.h>
28 #include <core/option.h>
29 
30 void
31 gf100_fb_intr(struct nvkm_fb *base)
32 {
33 	struct gf100_fb *fb = gf100_fb(base);
34 	struct nvkm_subdev *subdev = &fb->base.subdev;
35 	struct nvkm_device *device = subdev->device;
36 	u32 intr = nvkm_rd32(device, 0x000100);
37 	if (intr & 0x08000000)
38 		nvkm_debug(subdev, "PFFB intr\n");
39 	if (intr & 0x00002000)
40 		nvkm_debug(subdev, "PBFB intr\n");
41 }
42 
43 int
44 gf100_fb_oneinit(struct nvkm_fb *base)
45 {
46 	struct gf100_fb *fb = gf100_fb(base);
47 	struct nvkm_device *device = fb->base.subdev.device;
48 	int ret, size = 0x1000;
49 
50 	size = nvkm_longopt(device->cfgopt, "MmuDebugBufferSize", size);
51 	size = min(size, 0x1000);
52 
53 	ret = nvkm_memory_new(device, NVKM_MEM_TARGET_INST, size, 0x1000,
54 			      true, &fb->base.mmu_rd);
55 	if (ret)
56 		return ret;
57 
58 	ret = nvkm_memory_new(device, NVKM_MEM_TARGET_INST, size, 0x1000,
59 			      true, &fb->base.mmu_wr);
60 	if (ret)
61 		return ret;
62 
63 	fb->r100c10_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
64 	if (fb->r100c10_page) {
65 		fb->r100c10 = dma_map_page(device->dev, fb->r100c10_page, 0,
66 					   PAGE_SIZE, DMA_BIDIRECTIONAL);
67 		if (dma_mapping_error(device->dev, fb->r100c10))
68 			return -EFAULT;
69 	}
70 
71 	return 0;
72 }
73 
74 int
75 gf100_fb_init_page(struct nvkm_fb *fb)
76 {
77 	struct nvkm_device *device = fb->subdev.device;
78 	switch (fb->page) {
79 	case 16: nvkm_mask(device, 0x100c80, 0x00000001, 0x00000001); break;
80 	case 17: nvkm_mask(device, 0x100c80, 0x00000001, 0x00000000); break;
81 	default:
82 		return -EINVAL;
83 	}
84 	return 0;
85 }
86 
87 void
88 gf100_fb_init(struct nvkm_fb *base)
89 {
90 	struct gf100_fb *fb = gf100_fb(base);
91 	struct nvkm_device *device = fb->base.subdev.device;
92 
93 	if (fb->r100c10_page)
94 		nvkm_wr32(device, 0x100c10, fb->r100c10 >> 8);
95 }
96 
97 void *
98 gf100_fb_dtor(struct nvkm_fb *base)
99 {
100 	struct gf100_fb *fb = gf100_fb(base);
101 	struct nvkm_device *device = fb->base.subdev.device;
102 
103 	if (fb->r100c10_page) {
104 		dma_unmap_page(device->dev, fb->r100c10, PAGE_SIZE,
105 			       DMA_BIDIRECTIONAL);
106 		__free_page(fb->r100c10_page);
107 	}
108 
109 	return fb;
110 }
111 
112 int
113 gf100_fb_new_(const struct nvkm_fb_func *func, struct nvkm_device *device,
114 	      int index, struct nvkm_fb **pfb)
115 {
116 	struct gf100_fb *fb;
117 
118 	if (!(fb = kzalloc(sizeof(*fb), GFP_KERNEL)))
119 		return -ENOMEM;
120 	nvkm_fb_ctor(func, device, index, &fb->base);
121 	*pfb = &fb->base;
122 
123 	return 0;
124 }
125 
126 static const struct nvkm_fb_func
127 gf100_fb = {
128 	.dtor = gf100_fb_dtor,
129 	.oneinit = gf100_fb_oneinit,
130 	.init = gf100_fb_init,
131 	.init_page = gf100_fb_init_page,
132 	.intr = gf100_fb_intr,
133 	.ram_new = gf100_ram_new,
134 	.default_bigpage = 17,
135 };
136 
137 int
138 gf100_fb_new(struct nvkm_device *device, int index, struct nvkm_fb **pfb)
139 {
140 	return gf100_fb_new_(&gf100_fb, device, index, pfb);
141 }
142