Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0019  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
0020  * DEALINGS IN THE SOFTWARE.
0021  */
0022 #include <core/device.h>
0023 #include <core/firmware.h>
0024 
0025 int
0026 nvkm_firmware_load_name(const struct nvkm_subdev *subdev, const char *base,
0027             const char *name, int ver, const struct firmware **pfw)
0028 {
0029     char path[64];
0030     int ret;
0031 
0032     snprintf(path, sizeof(path), "%s%s", base, name);
0033     ret = nvkm_firmware_get(subdev, path, ver, pfw);
0034     if (ret < 0)
0035         return ret;
0036 
0037     return 0;
0038 }
0039 
0040 int
0041 nvkm_firmware_load_blob(const struct nvkm_subdev *subdev, const char *base,
0042             const char *name, int ver, struct nvkm_blob *blob)
0043 {
0044     const struct firmware *fw;
0045     int ret;
0046 
0047     ret = nvkm_firmware_load_name(subdev, base, name, ver, &fw);
0048     if (ret == 0) {
0049         blob->data = kmemdup(fw->data, fw->size, GFP_KERNEL);
0050         blob->size = fw->size;
0051         nvkm_firmware_put(fw);
0052         if (!blob->data)
0053             return -ENOMEM;
0054     }
0055 
0056     return ret;
0057 }
0058 
0059 /**
0060  * nvkm_firmware_get - load firmware from the official nvidia/chip/ directory
0061  * @subdev: subdevice that will use that firmware
0062  * @fwname: name of firmware file to load
0063  * @ver:    firmware version to load
0064  * @fw:     firmware structure to load to
0065  *
0066  * Use this function to load firmware files in the form nvidia/chip/fwname.bin.
0067  * Firmware files released by NVIDIA will always follow this format.
0068  */
0069 int
0070 nvkm_firmware_get(const struct nvkm_subdev *subdev, const char *fwname, int ver,
0071           const struct firmware **fw)
0072 {
0073     struct nvkm_device *device = subdev->device;
0074     char f[64];
0075     char cname[16];
0076     int i;
0077 
0078     /* Convert device name to lowercase */
0079     strncpy(cname, device->chip->name, sizeof(cname));
0080     cname[sizeof(cname) - 1] = '\0';
0081     i = strlen(cname);
0082     while (i) {
0083         --i;
0084         cname[i] = tolower(cname[i]);
0085     }
0086 
0087     if (ver != 0)
0088         snprintf(f, sizeof(f), "nvidia/%s/%s-%d.bin", cname, fwname, ver);
0089     else
0090         snprintf(f, sizeof(f), "nvidia/%s/%s.bin", cname, fwname);
0091 
0092     if (!firmware_request_nowarn(fw, f, device->dev)) {
0093         nvkm_debug(subdev, "firmware \"%s\" loaded - %zu byte(s)\n",
0094                f, (*fw)->size);
0095         return 0;
0096     }
0097 
0098     nvkm_debug(subdev, "firmware \"%s\" unavailable\n", f);
0099     return -ENOENT;
0100 }
0101 
0102 /*
0103  * nvkm_firmware_put - release firmware loaded with nvkm_firmware_get
0104  */
0105 void
0106 nvkm_firmware_put(const struct firmware *fw)
0107 {
0108     release_firmware(fw);
0109 }