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