Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2012 Red Hat Inc.
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice shall be included in
0012  * all copies or substantial portions of the Software.
0013  *
0014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0017  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0018  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0020  * OTHER DEALINGS IN THE SOFTWARE.
0021  *
0022  * Authors: Ben Skeggs
0023  */
0024 #include "priv.h"
0025 
0026 #include <subdev/bios.h>
0027 #include <subdev/bios/bmp.h>
0028 #include <subdev/bios/bit.h>
0029 #include <subdev/bios/image.h>
0030 
0031 static bool
0032 nvbios_addr(struct nvkm_bios *bios, u32 *addr, u8 size)
0033 {
0034     u32 p = *addr;
0035 
0036     if (*addr >= bios->image0_size && bios->imaged_addr) {
0037         *addr -= bios->image0_size;
0038         *addr += bios->imaged_addr;
0039     }
0040 
0041     if (unlikely(*addr + size > bios->size)) {
0042         nvkm_error(&bios->subdev, "OOB %d %08x %08x\n", size, p, *addr);
0043         return false;
0044     }
0045 
0046     return true;
0047 }
0048 
0049 u8
0050 nvbios_rd08(struct nvkm_bios *bios, u32 addr)
0051 {
0052     if (likely(nvbios_addr(bios, &addr, 1)))
0053         return bios->data[addr];
0054     return 0x00;
0055 }
0056 
0057 u16
0058 nvbios_rd16(struct nvkm_bios *bios, u32 addr)
0059 {
0060     if (likely(nvbios_addr(bios, &addr, 2)))
0061         return get_unaligned_le16(&bios->data[addr]);
0062     return 0x0000;
0063 }
0064 
0065 u32
0066 nvbios_rd32(struct nvkm_bios *bios, u32 addr)
0067 {
0068     if (likely(nvbios_addr(bios, &addr, 4)))
0069         return get_unaligned_le32(&bios->data[addr]);
0070     return 0x00000000;
0071 }
0072 
0073 u8
0074 nvbios_checksum(const u8 *data, int size)
0075 {
0076     u8 sum = 0;
0077     while (size--)
0078         sum += *data++;
0079     return sum;
0080 }
0081 
0082 u16
0083 nvbios_findstr(const u8 *data, int size, const char *str, int len)
0084 {
0085     int i, j;
0086 
0087     for (i = 0; i <= (size - len); i++) {
0088         for (j = 0; j < len; j++)
0089             if ((char)data[i + j] != str[j])
0090                 break;
0091         if (j == len)
0092             return i;
0093     }
0094 
0095     return 0;
0096 }
0097 
0098 int
0099 nvbios_memcmp(struct nvkm_bios *bios, u32 addr, const char *str, u32 len)
0100 {
0101     unsigned char c1, c2;
0102 
0103     while (len--) {
0104         c1 = nvbios_rd08(bios, addr++);
0105         c2 = *(str++);
0106         if (c1 != c2)
0107             return c1 - c2;
0108     }
0109     return 0;
0110 }
0111 
0112 int
0113 nvbios_extend(struct nvkm_bios *bios, u32 length)
0114 {
0115     if (bios->size < length) {
0116         u8 *prev = bios->data;
0117         if (!(bios->data = kmalloc(length, GFP_KERNEL))) {
0118             bios->data = prev;
0119             return -ENOMEM;
0120         }
0121         memcpy(bios->data, prev, bios->size);
0122         bios->size = length;
0123         kfree(prev);
0124         return 1;
0125     }
0126     return 0;
0127 }
0128 
0129 static void *
0130 nvkm_bios_dtor(struct nvkm_subdev *subdev)
0131 {
0132     struct nvkm_bios *bios = nvkm_bios(subdev);
0133     kfree(bios->data);
0134     return bios;
0135 }
0136 
0137 static const struct nvkm_subdev_func
0138 nvkm_bios = {
0139     .dtor = nvkm_bios_dtor,
0140 };
0141 
0142 int
0143 nvkm_bios_new(struct nvkm_device *device, enum nvkm_subdev_type type, int inst,
0144           struct nvkm_bios **pbios)
0145 {
0146     struct nvkm_bios *bios;
0147     struct nvbios_image image;
0148     struct bit_entry bit_i;
0149     int ret, idx = 0;
0150 
0151     if (!(bios = *pbios = kzalloc(sizeof(*bios), GFP_KERNEL)))
0152         return -ENOMEM;
0153     nvkm_subdev_ctor(&nvkm_bios, device, type, inst, &bios->subdev);
0154 
0155     ret = nvbios_shadow(bios);
0156     if (ret)
0157         return ret;
0158 
0159     /* Some tables have weird pointers that need adjustment before
0160      * they're dereferenced.  I'm not entirely sure why...
0161      */
0162     if (nvbios_image(bios, idx++, &image)) {
0163         bios->image0_size = image.size;
0164         while (nvbios_image(bios, idx++, &image)) {
0165             if (image.type == 0xe0) {
0166                 bios->imaged_addr = image.base;
0167                 break;
0168             }
0169         }
0170     }
0171 
0172     /* detect type of vbios we're dealing with */
0173     bios->bmp_offset = nvbios_findstr(bios->data, bios->size,
0174                       "\xff\x7f""NV\0", 5);
0175     if (bios->bmp_offset) {
0176         nvkm_debug(&bios->subdev, "BMP version %x.%x\n",
0177                bmp_version(bios) >> 8,
0178                bmp_version(bios) & 0xff);
0179     }
0180 
0181     bios->bit_offset = nvbios_findstr(bios->data, bios->size,
0182                       "\xff\xb8""BIT", 5);
0183     if (bios->bit_offset)
0184         nvkm_debug(&bios->subdev, "BIT signature found\n");
0185 
0186     /* determine the vbios version number */
0187     if (!bit_entry(bios, 'i', &bit_i) && bit_i.length >= 4) {
0188         bios->version.major = nvbios_rd08(bios, bit_i.offset + 3);
0189         bios->version.chip  = nvbios_rd08(bios, bit_i.offset + 2);
0190         bios->version.minor = nvbios_rd08(bios, bit_i.offset + 1);
0191         bios->version.micro = nvbios_rd08(bios, bit_i.offset + 0);
0192         bios->version.patch = nvbios_rd08(bios, bit_i.offset + 4);
0193     } else
0194     if (bmp_version(bios)) {
0195         bios->version.major = nvbios_rd08(bios, bios->bmp_offset + 13);
0196         bios->version.chip  = nvbios_rd08(bios, bios->bmp_offset + 12);
0197         bios->version.minor = nvbios_rd08(bios, bios->bmp_offset + 11);
0198         bios->version.micro = nvbios_rd08(bios, bios->bmp_offset + 10);
0199     }
0200 
0201     nvkm_info(&bios->subdev, "version %02x.%02x.%02x.%02x.%02x\n",
0202           bios->version.major, bios->version.chip,
0203           bios->version.minor, bios->version.micro, bios->version.patch);
0204     return 0;
0205 }