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 "priv.h"
25 #include "ram.h"
26 
27 #include <subdev/bios.h>
28 #include <subdev/bios/M0203.h>
29 #include <engine/gr.h>
30 #include <engine/mpeg.h>
31 
32 bool
33 nvkm_fb_memtype_valid(struct nvkm_fb *fb, u32 memtype)
34 {
35 	return fb->func->memtype_valid(fb, memtype);
36 }
37 
38 void
39 nvkm_fb_tile_fini(struct nvkm_fb *fb, int region, struct nvkm_fb_tile *tile)
40 {
41 	fb->func->tile.fini(fb, region, tile);
42 }
43 
44 void
45 nvkm_fb_tile_init(struct nvkm_fb *fb, int region, u32 addr, u32 size,
46 		  u32 pitch, u32 flags, struct nvkm_fb_tile *tile)
47 {
48 	fb->func->tile.init(fb, region, addr, size, pitch, flags, tile);
49 }
50 
51 void
52 nvkm_fb_tile_prog(struct nvkm_fb *fb, int region, struct nvkm_fb_tile *tile)
53 {
54 	struct nvkm_device *device = fb->subdev.device;
55 	fb->func->tile.prog(fb, region, tile);
56 	if (likely(device->gr))
57 		device->gr->engine.tile_prog(&device->gr->engine, region);
58 	if (likely(device->mpeg))
59 		device->mpeg->tile_prog(device->mpeg, region);
60 }
61 
62 int
63 nvkm_fb_bios_memtype(struct nvkm_bios *bios)
64 {
65 	struct nvkm_subdev *subdev = &bios->subdev;
66 	struct nvkm_device *device = subdev->device;
67 	const u8 ramcfg = (nvkm_rd32(device, 0x101000) & 0x0000003c) >> 2;
68 	struct nvbios_M0203E M0203E;
69 	u8 ver, hdr;
70 
71 	if (nvbios_M0203Em(bios, ramcfg, &ver, &hdr, &M0203E)) {
72 		switch (M0203E.type) {
73 		case M0203E_TYPE_DDR2 : return NVKM_RAM_TYPE_DDR2;
74 		case M0203E_TYPE_DDR3 : return NVKM_RAM_TYPE_DDR3;
75 		case M0203E_TYPE_GDDR3: return NVKM_RAM_TYPE_GDDR3;
76 		case M0203E_TYPE_GDDR5: return NVKM_RAM_TYPE_GDDR5;
77 		default:
78 			nvkm_warn(subdev, "M0203E type %02x\n", M0203E.type);
79 			return NVKM_RAM_TYPE_UNKNOWN;
80 		}
81 	}
82 
83 	nvkm_warn(subdev, "M0203E not matched!\n");
84 	return NVKM_RAM_TYPE_UNKNOWN;
85 }
86 
87 static void
88 nvkm_fb_intr(struct nvkm_subdev *subdev)
89 {
90 	struct nvkm_fb *fb = nvkm_fb(subdev);
91 	if (fb->func->intr)
92 		fb->func->intr(fb);
93 }
94 
95 static int
96 nvkm_fb_oneinit(struct nvkm_subdev *subdev)
97 {
98 	struct nvkm_fb *fb = nvkm_fb(subdev);
99 	if (fb->func->ram_new) {
100 		int ret = fb->func->ram_new(fb, &fb->ram);
101 		if (ret) {
102 			nvkm_error(subdev, "vram setup failed, %d\n", ret);
103 			return ret;
104 		}
105 	}
106 	return 0;
107 }
108 
109 static int
110 nvkm_fb_init(struct nvkm_subdev *subdev)
111 {
112 	struct nvkm_fb *fb = nvkm_fb(subdev);
113 	int ret, i;
114 
115 	if (fb->ram) {
116 		ret = nvkm_ram_init(fb->ram);
117 		if (ret)
118 			return ret;
119 	}
120 
121 	for (i = 0; i < fb->tile.regions; i++)
122 		fb->func->tile.prog(fb, i, &fb->tile.region[i]);
123 
124 	if (fb->func->init)
125 		fb->func->init(fb);
126 	return 0;
127 }
128 
129 static void *
130 nvkm_fb_dtor(struct nvkm_subdev *subdev)
131 {
132 	struct nvkm_fb *fb = nvkm_fb(subdev);
133 	int i;
134 
135 	for (i = 0; i < fb->tile.regions; i++)
136 		fb->func->tile.fini(fb, i, &fb->tile.region[i]);
137 
138 	nvkm_ram_del(&fb->ram);
139 
140 	if (fb->func->dtor)
141 		return fb->func->dtor(fb);
142 	return fb;
143 }
144 
145 static const struct nvkm_subdev_func
146 nvkm_fb = {
147 	.dtor = nvkm_fb_dtor,
148 	.oneinit = nvkm_fb_oneinit,
149 	.init = nvkm_fb_init,
150 	.intr = nvkm_fb_intr,
151 };
152 
153 void
154 nvkm_fb_ctor(const struct nvkm_fb_func *func, struct nvkm_device *device,
155 	     int index, struct nvkm_fb *fb)
156 {
157 	nvkm_subdev_ctor(&nvkm_fb, device, index, 0, &fb->subdev);
158 	fb->func = func;
159 	fb->tile.regions = fb->func->tile.regions;
160 }
161 
162 int
163 nvkm_fb_new_(const struct nvkm_fb_func *func, struct nvkm_device *device,
164 	     int index, struct nvkm_fb **pfb)
165 {
166 	if (!(*pfb = kzalloc(sizeof(**pfb), GFP_KERNEL)))
167 		return -ENOMEM;
168 	nvkm_fb_ctor(func, device, index, *pfb);
169 	return 0;
170 }
171