blob: 8a9ef209d4dabb9ec9b6802007baa1ce0f243ddd [file] [log] [blame]
Jani Nikula76be7492013-10-08 21:18:13 +03001/*
2 * Copyright © 2013 Intel Corporation
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 (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Jani Nikula <jani.nikula@intel.com>
25 *
26 */
27
28#include <errno.h>
29#include <fcntl.h>
30#include <getopt.h>
31#include <stdint.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <unistd.h>
36#include <sys/mman.h>
37#include <sys/stat.h>
38#include <sys/types.h>
39
40#include "intel_gpu_tools.h"
41
42#define OPREGION_HEADER_OFFSET 0
43#define OPREGION_ACPI_OFFSET 0x100
44#define OPREGION_SWSCI_OFFSET 0x200
45#define OPREGION_ASLE_OFFSET 0x300
46#define OPREGION_VBT_OFFSET 0x400
47#define OPREGION_ASLE_EXT_OFFSET 0x1C00
48
49#define MBOX_ACPI (1 << 0)
50#define MBOX_SWSCI (1 << 1)
51#define MBOX_ASLE (1 << 2)
52#define MBOX_VBT (1 << 3)
53#define MBOX_ASLE_EXT (1 << 4)
54
55struct opregion_header {
56 char sign[16];
57 uint32_t size;
58 uint32_t over;
59 char sver[32];
60 char vver[16];
61 char gver[16];
62 uint32_t mbox;
63 uint32_t dmod;
64 uint32_t pcon;
65 char dver[32];
66 uint8_t rsv1[124];
67} __attribute__((packed));
68
69/* OpRegion mailbox #1: public ACPI methods */
70struct opregion_acpi {
71 uint32_t drdy; /* driver readiness */
72 uint32_t csts; /* notification status */
73 uint32_t cevt; /* current event */
74 uint8_t rsvd1[20];
75 uint32_t didl[8]; /* supported display devices ID list */
76 uint32_t cpdl[8]; /* currently presented display list */
77 uint32_t cadl[8]; /* currently active display list */
78 uint32_t nadl[8]; /* next active devices list */
79 uint32_t aslp; /* ASL sleep time-out */
80 uint32_t tidx; /* toggle table index */
81 uint32_t chpd; /* current hotplug enable indicator */
82 uint32_t clid; /* current lid state*/
83 uint32_t cdck; /* current docking state */
84 uint32_t sxsw; /* Sx state resume */
85 uint32_t evts; /* ASL supported events */
86 uint32_t cnot; /* current OS notification */
87 uint32_t nrdy; /* driver status */
88 uint32_t did2[7];
89 uint32_t cpd2[7];
90 uint8_t rsvd2[4];
91} __attribute__((packed));
92
93/* OpRegion mailbox #2: SWSCI */
94struct opregion_swsci {
95 uint32_t scic; /* SWSCI command|status|data */
96 uint32_t parm; /* command parameters */
97 uint32_t dslp; /* driver sleep time-out */
98 uint8_t rsvd[244];
99} __attribute__((packed));
100
101/* OpRegion mailbox #3: ASLE */
102struct opregion_asle {
103 uint32_t ardy; /* driver readiness */
104 uint32_t aslc; /* ASLE interrupt command */
105 uint32_t tche; /* technology enabled indicator */
106 uint32_t alsi; /* current ALS illuminance reading */
107 uint32_t bclp; /* backlight brightness to set */
108 uint32_t pfit; /* panel fitting state */
109 uint32_t cblv; /* current brightness level */
110 uint16_t bclm[20]; /* backlight level duty cycle mapping table */
111 uint32_t cpfm; /* current panel fitting mode */
112 uint32_t epfm; /* enabled panel fitting modes */
113 uint8_t plut_header; /* panel LUT and identifier */
114 uint8_t plut_identifier[10]; /* panel LUT and identifier */
115 uint8_t plut[63]; /* panel LUT and identifier */
116 uint32_t pfmb; /* PWM freq and min brightness */
117 uint32_t ccdv;
118 uint32_t pcft;
119 uint32_t srot;
120 uint32_t iuer;
121 uint8_t fdss[8];
122 uint32_t fdsp;
123 uint32_t stat;
124 uint8_t rsvd[86];
125} __attribute__((packed));
126
127/* OpRegion mailbox #4: VBT */
128struct opregion_vbt {
129 char product_string[20];
130 /* rest ignored */
131} __attribute__((packed));
132
133/* OpRegion mailbox #5: ASLE extension */
134struct opregion_asle_ext {
135 uint32_t phed;
136 uint8_t bddc[256];
137} __attribute__((packed));
138
139static uint32_t decode_header(const void *buffer)
140{
141 const struct opregion_header *header = buffer;
142 char *s;
143
144 if (strncmp("IntelGraphicsMem", header->sign, sizeof(header->sign))) {
145 fprintf(stderr, "invalid opregion signature\n");
146 return 0;
147 }
148
149 printf("OpRegion Header:\n");
150
151 s = strndup(header->sign, sizeof(header->sign));
152 printf("\tsign:\t%s\n", s);
153 free(s);
154
155 printf("\tsize:\t0x%08x\n", header->size);
156 printf("\tover:\t0x%08x\n", header->over);
157
158 s = strndup(header->sver, sizeof(header->sver));
159 printf("\tsver:\t%s\n", s);
160 free(s);
161
162 s = strndup(header->vver, sizeof(header->vver));
163 printf("\tvver:\t%s\n", s);
164 free(s);
165
166 s = strndup(header->gver, sizeof(header->gver));
167 printf("\tgver:\t%s\n", s);
168 free(s);
169
170 printf("\tmbox:\t0x%08x\n", header->mbox);
171
172 printf("\tdmod:\t0x%08x\n", header->dmod);
173 printf("\tpcon:\t0x%08x\n", header->pcon);
174
175 s = strndup(header->dver, sizeof(header->dver));
176 printf("\tdver:\t%s\n", s);
177 free(s);
178
179 printf("\n");
180
181 return header->mbox;
182}
183
184static void decode_acpi(const void *buffer)
185{
186 const struct opregion_acpi *acpi = buffer;
187 int i;
188
189 printf("OpRegion Mailbox 1: Public ACPI Methods:\n");
190
191 printf("\tdrdy:\t0x%08x\n", acpi->drdy);
192 printf("\tcsts:\t0x%08x\n", acpi->csts);
193 printf("\tcevt:\t0x%08x\n", acpi->cevt);
194
195 printf("\tdidl:\n");
196 for (i = 0; i < ARRAY_SIZE(acpi->didl); i++)
197 printf("\t\tdidl[%d]:\t0x%08x\n", i, acpi->didl[i]);
198
199 printf("\tcpdl:\n");
200 for (i = 0; i < ARRAY_SIZE(acpi->cpdl); i++)
201 printf("\t\tcpdl[%d]:\t0x%08x\n", i, acpi->cpdl[i]);
202
203 printf("\tcadl:\n");
204 for (i = 0; i < ARRAY_SIZE(acpi->cadl); i++)
205 printf("\t\tcadl[%d]:\t0x%08x\n", i, acpi->cadl[i]);
206
207 printf("\tnadl:\n");
208 for (i = 0; i < ARRAY_SIZE(acpi->nadl); i++)
209 printf("\t\tnadl[%d]:\t0x%08x\n", i, acpi->nadl[i]);
210
211 printf("\taslp:\t0x%08x\n", acpi->aslp);
212 printf("\ttidx:\t0x%08x\n", acpi->tidx);
213 printf("\tchpd:\t0x%08x\n", acpi->chpd);
214 printf("\tclid:\t0x%08x\n", acpi->clid);
215 printf("\tcdck:\t0x%08x\n", acpi->cdck);
216 printf("\tsxsw:\t0x%08x\n", acpi->sxsw);
217 printf("\tevts:\t0x%08x\n", acpi->evts);
218 printf("\tcnot:\t0x%08x\n", acpi->cnot);
219 printf("\tnrdy:\t0x%08x\n", acpi->nrdy);
220
221 printf("\tdid2:\n");
222 for (i = 0; i < ARRAY_SIZE(acpi->did2); i++)
223 printf("\t\tdid2[%d]:\t0x%08x\n", i, acpi->did2[i]);
224
225 printf("\tcpd2:\n");
226 for (i = 0; i < ARRAY_SIZE(acpi->cpd2); i++)
227 printf("\t\tcpd2[%d]:\t0x%08x\n", i, acpi->cpd2[i]);
228
229 printf("\n");
230}
231
232static void decode_swsci(const void *buffer)
233{
234 const struct opregion_swsci *swsci = buffer;
235
236 printf("OpRegion Mailbox 2: Software SCI Interface (SWSCI):\n");
237
238 printf("\tscic:\t0x%08x\n", swsci->scic);
239 printf("\tparm:\t0x%08x\n", swsci->parm);
240 printf("\tdslp:\t0x%08x\n", swsci->dslp);
241
242 printf("\n");
243}
244
245static void decode_asle(const void *buffer)
246{
247 const struct opregion_asle *asle = buffer;
248 int i;
249
250 printf("OpRegion Mailbox 3: BIOS to Driver Notification (ASLE):\n");
251
252 printf("\tardy:\t0x%08x\n", asle->ardy);
253 printf("\taslc:\t0x%08x\n", asle->aslc);
254 printf("\ttche:\t0x%08x\n", asle->tche);
255 printf("\talsi:\t0x%08x\n", asle->alsi);
256 printf("\tbclp:\t0x%08x\n", asle->bclp);
257 printf("\tpfit:\t0x%08x\n", asle->pfit);
258 printf("\tcblv:\t0x%08x\n", asle->cblv);
259
260 printf("\tbclm:\n");
Jani Nikula6d3c9172013-10-08 21:18:14 +0300261 for (i = 0; i < ARRAY_SIZE(asle->bclm); i++) {
262 int valid = asle->bclm[i] & (1 << 15);
263 int percentage = (asle->bclm[i] & 0x7f00) >> 8;
264 int duty_cycle = asle->bclm[i] & 0xff;
265
266 printf("\t\tbclm[%d]:\t0x%04x", i, asle->bclm[i]);
267 if (valid)
268 printf(" (%3d%% -> 0x%02x)\n", percentage, duty_cycle);
269 else
270 printf("\n");
271
272 }
Jani Nikula76be7492013-10-08 21:18:13 +0300273
274 printf("\tcpfm:\t0x%08x\n", asle->cpfm);
275 printf("\tepfm:\t0x%08x\n", asle->epfm);
276
277 printf("\tplut header:\t0x%02x\n", asle->plut_header);
278
279 printf("\tplut identifier:");
280 for (i = 0; i < ARRAY_SIZE(asle->plut_identifier); i++)
281 printf(" %02x", asle->plut_identifier[i]);
282 printf("\n");
283
284 printf("\tplut:\n");
285 for (i = 0; i < ARRAY_SIZE(asle->plut); i++) {
286 const int COLUMNS = 7;
287
288 if (i % COLUMNS == 0)
289 printf("\t\tplut[%d]:\t", i / COLUMNS);
290
291 printf("%02x ", asle->plut[i]);
292
293 if (i % COLUMNS == COLUMNS - 1)
294 printf("\n");
295 }
296
297 printf("\tpfmb:\t0x%08x\n", asle->pfmb);
298 printf("\tccdv:\t0x%08x\n", asle->ccdv);
299 printf("\tpcft:\t0x%08x\n", asle->pcft);
300 printf("\tsrot:\t0x%08x\n", asle->srot);
301 printf("\tiuer:\t0x%08x\n", asle->iuer);
302
303 printf("\tfdss:\t");
304 for (i = 0; i < ARRAY_SIZE(asle->fdss); i++)
305 printf("%02x ", asle->fdss[i]);
306 printf("\n");
307
308 printf("\tfdsp:\t0x%08x\n", asle->fdsp);
309 printf("\tstat:\t0x%08x\n", asle->stat);
310
311 printf("\n");
312}
313
314static void decode_vbt(const void *buffer)
315{
316 const struct opregion_vbt *vbt = buffer;
317 char *s;
318
319 printf("OpRegion Mailbox 4: Video BIOS Table (VBT):\n");
320
321 s = strndup(vbt->product_string, sizeof(vbt->product_string));
322 printf("\tproduct string:\t%s\n", s);
323 free(s);
324
325 printf("\t(use intel_bios_reader to decode the VBT)\n");
326
327 printf("\n");
328}
329
330static void decode_asle_ext(const void *buffer)
331{
332 const struct opregion_asle_ext *asle_ext = buffer;
333 int i;
334
335 printf("OpRegion Mailbox 5: BIOS to Driver Notification Extension:\n");
336
337 printf("\tphed:\t0x%08x\n", asle_ext->phed);
338
339 printf("\tbddc:\n");
340 for (i = 0; i < ARRAY_SIZE(asle_ext->bddc); i++) {
341 const int COLUMNS = 16;
342
343 if (i % COLUMNS == 0)
344 printf("\t\tbddc[0x%02x]:\t", i);
345
346 printf("%02x ", asle_ext->bddc[i]);
347
348 if (i % COLUMNS == COLUMNS - 1)
349 printf("\n");
350 }
351
352 printf("\n");
353}
354
355static void decode_opregion(const uint8_t *opregion, int size)
356{
357 uint32_t mbox;
358
359 /* XXX: allow decoding up to size */
360 if (OPREGION_ASLE_EXT_OFFSET + sizeof(struct opregion_asle_ext) > size) {
361 fprintf(stderr, "buffer too small\n");
362 return;
363 }
364
365 mbox = decode_header(opregion + OPREGION_HEADER_OFFSET);
366 if (mbox & MBOX_ACPI)
367 decode_acpi(opregion + OPREGION_ACPI_OFFSET);
368 if (mbox & MBOX_SWSCI)
369 decode_swsci(opregion + OPREGION_SWSCI_OFFSET);
370 if (mbox & MBOX_ASLE)
371 decode_asle(opregion + OPREGION_ASLE_OFFSET);
372 if (mbox & MBOX_VBT)
373 decode_vbt(opregion + OPREGION_VBT_OFFSET);
374 if (mbox & MBOX_ASLE_EXT)
375 decode_asle_ext(opregion + OPREGION_ASLE_EXT_OFFSET);
376}
377
378int main(int argc, char *argv[])
379{
380 const char *filename = "/sys/kernel/debug/dri/0/i915_opregion";
381 int fd;
382 struct stat finfo;
383 uint8_t *opregion;
384 int c, option_index = 0;
385
386 static struct option long_options[] = {
387 { "file", required_argument, 0, 'f' },
388 { "help", no_argument, 0, 'h' },
389 { 0 },
390 };
391
392 while ((c = getopt_long(argc, argv, "hf:",
393 long_options, &option_index)) != -1) {
394 switch (c) {
395 case 'h':
396 printf("usage: intel_opregion_decode [-f|--file=<input>]\n");
397 return 0;
398 case 'f':
399 filename = optarg;
400 break;
401 default:
402 fprintf(stderr, "unkown command options\n");
403 return 1;
404 }
405 }
406
407 fd = open(filename, O_RDONLY);
408 if (fd == -1) {
409 printf("Couldn't open \"%s\": %s\n", filename, strerror(errno));
410 return 1;
411 }
412
413 if (stat(filename, &finfo)) {
414 printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
415 return 1;
416 }
417
418 if (finfo.st_size == 0) {
419 int len = 0, ret;
420 finfo.st_size = 8192;
421 opregion = malloc(finfo.st_size);
422 while ((ret = read(fd, opregion + len, finfo.st_size - len))) {
423 if (ret < 0) {
424 printf("failed to read \"%s\": %s\n", filename,
425 strerror(errno));
426 return 1;
427 }
428
429 len += ret;
430 if (len == finfo.st_size) {
431 finfo.st_size *= 2;
432 opregion = realloc(opregion, finfo.st_size);
433 }
434 }
435 } else {
436 opregion = mmap(NULL, finfo.st_size, PROT_READ, MAP_SHARED,
437 fd, 0);
438 if (opregion == MAP_FAILED) {
439 printf("failed to map \"%s\": %s\n", filename,
440 strerror(errno));
441 return 1;
442 }
443 }
444
445 decode_opregion(opregion, finfo.st_size);
446
447 return 0;
448}