1f26493d2SKarol Herbst /*
2f26493d2SKarol Herbst * Copyright 2016 Karol Herbst
3f26493d2SKarol Herbst *
4f26493d2SKarol Herbst * Permission is hereby granted, free of charge, to any person obtaining a
5f26493d2SKarol Herbst * copy of this software and associated documentation files (the "Software"),
6f26493d2SKarol Herbst * to deal in the Software without restriction, including without limitation
7f26493d2SKarol Herbst * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8f26493d2SKarol Herbst * and/or sell copies of the Software, and to permit persons to whom the
9f26493d2SKarol Herbst * Software is furnished to do so, subject to the following conditions:
10f26493d2SKarol Herbst *
11f26493d2SKarol Herbst * The above copyright notice and this permission notice shall be included in
12f26493d2SKarol Herbst * all copies or substantial portions of the Software.
13f26493d2SKarol Herbst *
14f26493d2SKarol Herbst * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15f26493d2SKarol Herbst * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16f26493d2SKarol Herbst * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17f26493d2SKarol Herbst * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18f26493d2SKarol Herbst * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19f26493d2SKarol Herbst * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20f26493d2SKarol Herbst * OTHER DEALINGS IN THE SOFTWARE.
21f26493d2SKarol Herbst *
22f26493d2SKarol Herbst * Authors: Karol Herbst
23f26493d2SKarol Herbst */
24f26493d2SKarol Herbst #include <subdev/bios.h>
25f26493d2SKarol Herbst #include <subdev/bios/bit.h>
26f26493d2SKarol Herbst #include <subdev/bios/vpstate.h>
27f26493d2SKarol Herbst
28f26493d2SKarol Herbst static u32
nvbios_vpstate_offset(struct nvkm_bios * b)29f26493d2SKarol Herbst nvbios_vpstate_offset(struct nvkm_bios *b)
30f26493d2SKarol Herbst {
31f26493d2SKarol Herbst struct bit_entry bit_P;
32f26493d2SKarol Herbst
33f26493d2SKarol Herbst if (!bit_entry(b, 'P', &bit_P)) {
34a7cb78baSBen Skeggs if (bit_P.version == 2 && bit_P.length >= 0x3c)
35f26493d2SKarol Herbst return nvbios_rd32(b, bit_P.offset + 0x38);
36f26493d2SKarol Herbst }
37f26493d2SKarol Herbst
38f26493d2SKarol Herbst return 0x0000;
39f26493d2SKarol Herbst }
40f26493d2SKarol Herbst
41f26493d2SKarol Herbst int
nvbios_vpstate_parse(struct nvkm_bios * b,struct nvbios_vpstate_header * h)42f26493d2SKarol Herbst nvbios_vpstate_parse(struct nvkm_bios *b, struct nvbios_vpstate_header *h)
43f26493d2SKarol Herbst {
44f26493d2SKarol Herbst if (!h)
45f26493d2SKarol Herbst return -EINVAL;
46f26493d2SKarol Herbst
47f26493d2SKarol Herbst h->offset = nvbios_vpstate_offset(b);
48f26493d2SKarol Herbst if (!h->offset)
49f26493d2SKarol Herbst return -ENODEV;
50f26493d2SKarol Herbst
51f26493d2SKarol Herbst h->version = nvbios_rd08(b, h->offset);
52f26493d2SKarol Herbst switch (h->version) {
53f26493d2SKarol Herbst case 0x10:
54f26493d2SKarol Herbst h->hlen = nvbios_rd08(b, h->offset + 0x1);
55f26493d2SKarol Herbst h->elen = nvbios_rd08(b, h->offset + 0x2);
56f26493d2SKarol Herbst h->slen = nvbios_rd08(b, h->offset + 0x3);
57f26493d2SKarol Herbst h->scount = nvbios_rd08(b, h->offset + 0x4);
58f26493d2SKarol Herbst h->ecount = nvbios_rd08(b, h->offset + 0x5);
59f26493d2SKarol Herbst
60f26493d2SKarol Herbst h->base_id = nvbios_rd08(b, h->offset + 0x0f);
61*f706037cSKarol Herbst if (h->hlen > 0x10)
62f26493d2SKarol Herbst h->boost_id = nvbios_rd08(b, h->offset + 0x10);
63*f706037cSKarol Herbst else
64*f706037cSKarol Herbst h->boost_id = 0xff;
65*f706037cSKarol Herbst if (h->hlen > 0x11)
66f26493d2SKarol Herbst h->tdp_id = nvbios_rd08(b, h->offset + 0x11);
67*f706037cSKarol Herbst else
68*f706037cSKarol Herbst h->tdp_id = 0xff;
69f26493d2SKarol Herbst return 0;
70f26493d2SKarol Herbst default:
71f26493d2SKarol Herbst return -EINVAL;
72f26493d2SKarol Herbst }
73f26493d2SKarol Herbst }
74f26493d2SKarol Herbst
75f26493d2SKarol Herbst int
nvbios_vpstate_entry(struct nvkm_bios * b,struct nvbios_vpstate_header * h,u8 idx,struct nvbios_vpstate_entry * e)76f26493d2SKarol Herbst nvbios_vpstate_entry(struct nvkm_bios *b, struct nvbios_vpstate_header *h,
77f26493d2SKarol Herbst u8 idx, struct nvbios_vpstate_entry *e)
78f26493d2SKarol Herbst {
79f26493d2SKarol Herbst u32 offset;
80f26493d2SKarol Herbst
81f26493d2SKarol Herbst if (!e || !h || idx > h->ecount)
82f26493d2SKarol Herbst return -EINVAL;
83f26493d2SKarol Herbst
84f26493d2SKarol Herbst offset = h->offset + h->hlen + idx * (h->elen + (h->slen * h->scount));
85f26493d2SKarol Herbst e->pstate = nvbios_rd08(b, offset);
86f26493d2SKarol Herbst e->clock_mhz = nvbios_rd16(b, offset + 0x5);
87f26493d2SKarol Herbst return 0;
88f26493d2SKarol Herbst }
89