File Index
Files and directories
Sign in
FILE

base.c

File preview with syntax highlighting.

linux-6.19.14/drivers/gpu/drm/nouveau/nvkm/subdev/bios/base.c C
  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
 26#include <subdev/bios.h>
 27#include <subdev/bios/bmp.h>
 28#include <subdev/bios/bit.h>
 29#include <subdev/bios/image.h>
 30
 31static bool
 32nvbios_addr(struct nvkm_bios *bios, u32 *addr, u8 size)
 33{
 34	u32 p = *addr;
 35
 36	if (*addr >= bios->image0_size && bios->imaged_addr) {
 37		*addr -= bios->image0_size;
 38		*addr += bios->imaged_addr;
 39	}
 40
 41	if (unlikely(*addr + size > bios->size)) {
 42		nvkm_error(&bios->subdev, "OOB %d %08x %08x\n", size, p, *addr);
 43		return false;
 44	}
 45
 46	return true;
 47}
 48
 49void *
 50nvbios_pointer(struct nvkm_bios *bios, u32 addr)
 51{
 52	if (likely(nvbios_addr(bios, &addr, 0)))
 53		return &bios->data[addr];
 54	return NULL;
 55}
 56
 57u8
 58nvbios_rd08(struct nvkm_bios *bios, u32 addr)
 59{
 60	if (likely(nvbios_addr(bios, &addr, 1)))
 61		return bios->data[addr];
 62	return 0x00;
 63}
 64
 65u16
 66nvbios_rd16(struct nvkm_bios *bios, u32 addr)
 67{
 68	if (likely(nvbios_addr(bios, &addr, 2)))
 69		return get_unaligned_le16(&bios->data[addr]);
 70	return 0x0000;
 71}
 72
 73u32
 74nvbios_rd32(struct nvkm_bios *bios, u32 addr)
 75{
 76	if (likely(nvbios_addr(bios, &addr, 4)))
 77		return get_unaligned_le32(&bios->data[addr]);
 78	return 0x00000000;
 79}
 80
 81u8
 82nvbios_checksum(const u8 *data, int size)
 83{
 84	u8 sum = 0;
 85	while (size--)
 86		sum += *data++;
 87	return sum;
 88}
 89
 90u16
 91nvbios_findstr(const u8 *data, int size, const char *str, int len)
 92{
 93	int i, j;
 94
 95	for (i = 0; i <= (size - len); i++) {
 96		for (j = 0; j < len; j++)
 97			if ((char)data[i + j] != str[j])
 98				break;
 99		if (j == len)
100			return i;
101	}
102
103	return 0;
104}
105
106int
107nvbios_memcmp(struct nvkm_bios *bios, u32 addr, const char *str, u32 len)
108{
109	unsigned char c1, c2;
110
111	while (len--) {
112		c1 = nvbios_rd08(bios, addr++);
113		c2 = *(str++);
114		if (c1 != c2)
115			return c1 - c2;
116	}
117	return 0;
118}
119
120int
121nvbios_extend(struct nvkm_bios *bios, u32 length)
122{
123	if (bios->size < length) {
124		u8 *prev = bios->data;
125		if (!(bios->data = kmalloc(length, GFP_KERNEL))) {
126			bios->data = prev;
127			return -ENOMEM;
128		}
129		memcpy(bios->data, prev, bios->size);
130		bios->size = length;
131		kfree(prev);
132		return 1;
133	}
134	return 0;
135}
136
137static void *
138nvkm_bios_dtor(struct nvkm_subdev *subdev)
139{
140	struct nvkm_bios *bios = nvkm_bios(subdev);
141	kfree(bios->data);
142	return bios;
143}
144
145static const struct nvkm_subdev_func
146nvkm_bios = {
147	.dtor = nvkm_bios_dtor,
148};
149
150int
151nvkm_bios_new(struct nvkm_device *device, enum nvkm_subdev_type type, int inst,
152	      struct nvkm_bios **pbios)
153{
154	struct nvkm_bios *bios;
155	struct nvbios_image image;
156	struct bit_entry bit_i;
157	int ret, idx = 0;
158
159	if (!(bios = *pbios = kzalloc(sizeof(*bios), GFP_KERNEL)))
160		return -ENOMEM;
161	nvkm_subdev_ctor(&nvkm_bios, device, type, inst, &bios->subdev);
162
163	ret = nvbios_shadow(bios);
164	if (ret)
165		return ret;
166
167	/* Some tables have weird pointers that need adjustment before
168	 * they're dereferenced.  I'm not entirely sure why...
169	 */
170	if (nvbios_image(bios, idx++, &image)) {
171		bios->image0_size = image.size;
172		while (nvbios_image(bios, idx++, &image)) {
173			if (image.type == 0xe0) {
174				bios->imaged_addr = image.base;
175				break;
176			}
177		}
178	}
179
180	/* detect type of vbios we're dealing with */
181	bios->bmp_offset = nvbios_findstr(bios->data, bios->size,
182					  "\xff\x7f""NV\0", 5);
183	if (bios->bmp_offset) {
184		nvkm_debug(&bios->subdev, "BMP version %x.%x\n",
185			   bmp_version(bios) >> 8,
186			   bmp_version(bios) & 0xff);
187	}
188
189	bios->bit_offset = nvbios_findstr(bios->data, bios->size,
190					  "\xff\xb8""BIT", 5);
191	if (bios->bit_offset)
192		nvkm_debug(&bios->subdev, "BIT signature found\n");
193
194	/* determine the vbios version number */
195	if (!bit_entry(bios, 'i', &bit_i) && bit_i.length >= 4) {
196		bios->version.major = nvbios_rd08(bios, bit_i.offset + 3);
197		bios->version.chip  = nvbios_rd08(bios, bit_i.offset + 2);
198		bios->version.minor = nvbios_rd08(bios, bit_i.offset + 1);
199		bios->version.micro = nvbios_rd08(bios, bit_i.offset + 0);
200		bios->version.patch = nvbios_rd08(bios, bit_i.offset + 4);
201	} else
202	if (bmp_version(bios)) {
203		bios->version.major = nvbios_rd08(bios, bios->bmp_offset + 13);
204		bios->version.chip  = nvbios_rd08(bios, bios->bmp_offset + 12);
205		bios->version.minor = nvbios_rd08(bios, bios->bmp_offset + 11);
206		bios->version.micro = nvbios_rd08(bios, bios->bmp_offset + 10);
207	}
208
209	nvkm_info(&bios->subdev, "version %02x.%02x.%02x.%02x.%02x\n",
210		  bios->version.major, bios->version.chip,
211		  bios->version.minor, bios->version.micro, bios->version.patch);
212	return 0;
213}
~/ Read-only file preview.