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