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