~emersion/drmdb

ref: a9bed291adfea7d9bfa584c07b0557a48170363e drmdb/drmtree/drmtree.go -rw-r--r-- 12.3 KiB
a9bed291Simon Ser Add device page 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Package drmtree exposes DRM data as a tree structure.
package drmtree

import (
	"encoding/json"
	"fmt"
	"strings"

	"git.sr.ht/~emersion/go-drm"
	"git.sr.ht/~emersion/drmdb/treefmt"
)

type DriverVersion struct {
	Major int32  `json:"major"`
	Minor int32  `json:"minor"`
	Patch int32  `json:"patch"`
	Date  string `json:"date"`
}

func (ver *DriverVersion) String() string {
	return fmt.Sprintf("%v.%v.%v (%v)", ver.Major, ver.Minor, ver.Patch, ver.Date)
}

func (ver *DriverVersion) Less(other *DriverVersion) bool {
	return ver.Major < other.Major || ver.Minor < other.Minor || ver.Patch < other.Patch || ver.Date < other.Date
}

type Kernel struct {
	SysName string `json:"sysname"`
	Release string `json:"release"`
	Version string `json:"version"`
}

func (k *Kernel) String() string {
	if k.SysName == "" {
		return "unknown kernel"
	}
	s := k.SysName
	if k.Release != "" {
		s += " " + k.Release
	}
	return s
}

type Driver struct {
	Name       string             `json:"name"`
	Desc       string             `json:"desc"`
	Version    DriverVersion      `json:"version"`
	Kernel     Kernel             `json:"kernel"`
	Caps       map[string]*uint64 `json:"caps"`
	ClientCaps map[string]bool    `json:"client_caps"`
}

func (drv *Driver) FormatTree(tf treefmt.Formatter) {
	tf.Printf("Driver: %v (%v) version %v on %v", drv.Name, drv.Desc, &drv.Version, &drv.Kernel)
	tfc := tf.NewChild()

	for c, v := range drv.Caps {
		if v != nil {
			if c == "PRIME" {
				tfc.Printf("DRM_CAP_PRIME supported")
				tfcc := tfc.NewChild()
				tfcc.Printf("DRM_CAP_PRIME_IMPORT = %v", *v&drm.CapPrimeImport != 0)
				tfcc.Printf("DRM_CAP_PRIME_EXPORT = %v", *v&drm.CapPrimeExport != 0)
			} else {
				tfc.Printf("DRM_CAP_%v = %v", c, *v)
			}
		} else {
			tfc.Printf("DRM_CAP_%v unsupported", c)
		}
	}

	for c, ok := range drv.ClientCaps {
		if ok {
			tfc.Printf("DRM_CLIENT_CAP_%v supported", c)
		} else {
			tfc.Printf("DRM_CLIENT_CAP_%v unsupported", c)
		}
	}
}

type DevicePCI struct {
	Vendor    uint32 `json:"vendor"`
	Device    uint32 `json:"device"`
	SubVendor uint32 `json:"subsystem_vendor"`
	SubDevice uint32 `json:"subsystem_device"`
}

type DevicePlatform struct {
	Compatible []string `json:"compatible"`
}

type Device struct {
	BusType    drm.BusType `json:"bus_type"`
	DeviceData interface{} `json:"device_data,omitempty"`
}

func (dev *Device) UnmarshalJSON(b []byte) error {
	type device Device

	var deviceData json.RawMessage
	rawDev := device{DeviceData: &deviceData}
	if err := json.Unmarshal(b, &rawDev); err != nil {
		return err
	}

	switch rawDev.BusType {
	case drm.BusPCI:
		var devPCI DevicePCI
		if err := json.Unmarshal(deviceData, &devPCI); err != nil {
			return err
		}
		rawDev.DeviceData = &devPCI
	case drm.BusPlatform:
		var devPlatform DevicePlatform
		if err := json.Unmarshal(deviceData, &devPlatform); err != nil {
			return err
		}
		rawDev.DeviceData = &devPlatform
	default:
		rawDev.DeviceData = nil
	}

	*dev = Device(rawDev)
	return nil
}

func (dev *Device) BusID() string {
	switch dev := dev.DeviceData.(type) {
	case *DevicePCI:
		return fmt.Sprintf("%04X:%04X", dev.Vendor, dev.Device)
	case *DevicePlatform:
		return strings.Join(dev.Compatible, "+")
	default:
		return ""
	}
}

func (dev *Device) FormatTree(tf treefmt.Formatter) {
	tf.Printf("Device: %v %v", dev.BusType, dev.BusID())
}

type Mode struct {
	Clock      uint32 `json:"clock"`
	HDisplay   uint16 `json:"hdisplay"`
	HSyncStart uint16 `json:"hsync_start"`
	HSyncEnd   uint16 `json:"hsync_end"`
	HTotal     uint16 `json:"htotal"`
	HSkew      uint16 `json:"hskew"`
	VDisplay   uint16 `json:"vdisplay"`
	VSyncStart uint16 `json:"vsync_total"`
	VSyncEnd   uint16 `json:"vsync_end"`
	VTotal     uint16 `json:"vtotal"`
	VScan      uint16 `json:"vscan"`
	VRefresh   uint32 `json:"vrefresh"`
	Flags      uint32 `json:"flags"`
	Type       uint32 `json:"type"`
	Name       string `json:"name"`
}

func (mode *Mode) String() string {
	// TODO: refresh and flags
	return mode.Name
}

type PlaneInFormatsModifier struct {
	Modifier drm.Modifier `json:"modifier"`
	Formats  []drm.Format `json:"formats"`
}

type PropertySpecEnum struct {
	Name  string `json:"name"`
	Value uint64 `json:"value"`
}

type PropertySpecRange struct {
	Min uint64 `json:"min"`
	Max uint64 `json:"max`
}

type PropertySpecSignedRange struct {
	Min int64 `json:"min"`
	Max int64 `json:"max`
}

type Property struct {
	ID        drm.PropertyID   `json:"id"`
	Type      drm.PropertyType `json:"type"`
	Immutable bool             `json:"immutable"`
	Atomic    bool             `json:"atomic"`
	Spec      interface{}      `json:"spec"`
	RawValue  uint64           `json:"raw_value"`
	// Value interpreted with the property type
	Value interface{} `json:"value"`
	// Value interpreted with the property name, optional
	Data interface{} `json:"data"`

	// For Data unmarshaling, populated by PropertyMap.UnmarshalJSON
	name string
}

func (prop *Property) UnmarshalJSON(b []byte) error {
	type property Property

	var rawSpec, rawValue, rawData json.RawMessage
	rawProp := property{
		Spec:  &rawSpec,
		Value: &rawValue,
		Data:  &rawData,
	}
	if err := json.Unmarshal(b, &rawProp); err != nil {
		return err
	}

	rawProp.Spec = nil
	rawProp.Value = nil
	rawProp.Data = nil

	if rawSpec != nil {
		var err error
		switch rawProp.Type {
		case drm.PropertyRange:
			var spec PropertySpecRange
			err = json.Unmarshal(rawSpec, &spec)
			rawProp.Spec = &spec
		case drm.PropertyEnum, drm.PropertyBitmask:
			var spec []PropertySpecEnum
			err = json.Unmarshal(rawSpec, &spec)
			rawProp.Spec = spec
		case drm.PropertyObject:
			spec := drm.ObjectType(0)
			err = json.Unmarshal(rawSpec, &spec)
			rawProp.Spec = spec
		case drm.PropertySignedRange:
			spec := PropertySpecSignedRange{}
			err = json.Unmarshal(rawSpec, &spec)
			rawProp.Spec = &spec
		}
		if err != nil {
			return err
		}
	}

	if rawValue != nil {
		var err error
		switch rawProp.Type {
		case drm.PropertyRange, drm.PropertyEnum, drm.PropertyBitmask:
			var value uint64
			err = json.Unmarshal(rawValue, &value)
			rawProp.Value = value
		case drm.PropertyBlob:
			var value []byte
			err = json.Unmarshal(rawValue, &value)
			rawProp.Value = value
		case drm.PropertyObject:
			var value drm.ObjectID
			err = json.Unmarshal(rawValue, &value)
			rawProp.Value = value
		case drm.PropertySignedRange:
			var value int64
			err = json.Unmarshal(rawValue, &value)
			rawProp.Value = value
		}
		if err != nil {
			return err
		}
	}

	if rawData != nil {
		var err error
		switch prop.name {
		case "SRC_X", "SRC_Y", "SRC_W", "SRC_H":
			var data uint64
			err = json.Unmarshal(rawData, &data)
			rawProp.Data = data
		case "IN_FORMATS":
			var data []PlaneInFormatsModifier
			err = json.Unmarshal(rawData, &data)
			rawProp.Data = data
		case "MODE_ID":
			var data Mode
			err = json.Unmarshal(rawData, &data)
			rawProp.Data = &data
		case "WRITEBACK_PIXEL_FORMATS":
			var data []drm.Format
			err = json.Unmarshal(rawData, &data)
			rawProp.Data = data
		case "PATH":
			var data string
			err = json.Unmarshal(rawData, &data)
			rawProp.Data = data
		}
		if err != nil {
			return err
		}
	}

	*prop = Property(rawProp)
	return nil
}

type PropertyMap map[string]Property

func (m *PropertyMap) UnmarshalJSON(b []byte) error {
	var rawMap map[string]json.RawMessage
	if err := json.Unmarshal(b, &rawMap); err != nil {
		return err
	}

	*m = make(PropertyMap)

	for name, rawProp := range rawMap {
		prop := Property{name: name}
		if err := json.Unmarshal(rawProp, &prop); err != nil {
			return err
		}

		(*m)[name] = prop
	}

	return nil
}

func (m *PropertyMap) FormatTree(tf treefmt.Formatter) {
	for name, prop := range *m {
		// TODO: immutable, atomic
		// TODO: type-specific property data
		val := prop.Data
		if val == nil {
			val = prop.Value
		}
		tf.Printf("%q: %v = %v", name, prop.Type, val)
	}
}

type Connector struct {
	ID         drm.ConnectorID     `json:"id"`
	Type       drm.ConnectorType   `json:"type"`
	Status     drm.ConnectorStatus `json:"status"`
	PhyWidth   uint32              `json:"phy_width"`
	PhyHeight  uint32              `json:"phy_height"`
	Subpixel   drm.Subpixel        `json:"subpixel"`
	Encoders   []drm.EncoderID     `json:"encoders"`
	Modes      []Mode              `json:"modes"`
	Properties PropertyMap         `json:"properties"`
}

func encoderIDsString(encs []drm.EncoderID) string {
	s := "{"
	for i, id := range encs {
		if i != 0 {
			s += ", "
		}
		s += fmt.Sprintf("%v", id)
	}
	s += "}"
	return s
}

func (conn *Connector) FormatTree(tf treefmt.Formatter) {
	tf.Printf("Object ID: %v", conn.ID)
	tf.Printf("Type: %v", conn.Type)
	tf.Printf("Status: %v", conn.Status)
	if conn.Status == drm.ConnectorStatusConnected {
		tf.Printf("Physical size: %vx%v mm", conn.PhyWidth, conn.PhyHeight)
		tf.Printf("Subpixel: %v", conn.Subpixel)
	}
	tf.Printf("Encoders: %v", encoderIDsString(conn.Encoders))
	if len(conn.Modes) > 0 {
		tf.Printf("Modes")
		tfc := tf.NewChild()
		for _, m := range conn.Modes {
			tfc.Printf("%v", &m)
		}
	}
	if len(conn.Properties) > 0 {
		tf.Printf("Properties")
		conn.Properties.FormatTree(tf.NewChild())
	}
}

type Encoder struct {
	ID             drm.EncoderID   `json:"id"`
	Type           drm.EncoderType `json:"type"`
	CRTC           drm.CRTCID      `json:"crtc"`
	PossibleCRTCs  uint32          `json:"possible_crtcs"`
	PossibleClones uint32          `json:"possible_clones"`
}

func bitfieldString(v uint32) string {
	s := "{"
	first := true
	for i := 0; i < 32; i++ {
		if v&(1<<uint(i)) != 0 {
			if !first {
				s += ", "
			}
			s += fmt.Sprintf("%v", i)
			first = false
		}
	}
	s += "}"
	return s
}

func (enc *Encoder) FormatTree(tf treefmt.Formatter) {
	tf.Printf("Object ID: %v", enc.ID)
	tf.Printf("Type: %v", enc.Type)
	tf.Printf("CRTCs: %v", bitfieldString(enc.PossibleCRTCs))
	tf.Printf("Clones: %v", bitfieldString(enc.PossibleClones))
}

type CRTC struct {
	ID         drm.CRTCID  `json:"id"`
	FB         drm.FBID    `json:"fb"`
	X          uint32      `json:"x"`
	Y          uint32      `json:"y"`
	GammaSize  uint32      `json:"gamma_size"`
	Mode       *Mode       `json:"mode"`
	Properties PropertyMap `json:"properties"`
}

func (crtc *CRTC) FormatTree(tf treefmt.Formatter) {
	tf.Printf("Object ID: %v", crtc.ID)
	tf.Printf("FB: %v", crtc.FB)
	tf.Printf("Position: %v, %v", crtc.X, crtc.Y)
	tf.Printf("Gamma size: %v", crtc.GammaSize)
	if crtc.Mode != nil {
		tf.Printf("Mode: %v", crtc.Mode)
	}
	if len(crtc.Properties) > 0 {
		tf.Printf("Properties")
		crtc.Properties.FormatTree(tf.NewChild())
	}
}

type Plane struct {
	ID            drm.PlaneID  `json:"id"`
	CRTC          drm.CRTCID   `json:"crtc"`
	FB            drm.FBID     `json:"fb"`
	PossibleCRTCs uint32       `json:"possible_crtcs"`
	GammaSize     uint32       `json:"gamma_size"`
	Formats       []drm.Format `json:"formats"`
	Properties    PropertyMap  `json:"properties"`
}

func (plane *Plane) FormatTree(tf treefmt.Formatter) {
	tf.Printf("Object ID: %v", plane.ID)
	tf.Printf("CRTC: %v", plane.CRTC)
	tf.Printf("FB: %v", plane.FB)
	tf.Printf("CRTCs: %v", bitfieldString(plane.PossibleCRTCs))
	tf.Printf("Gamma size: %v", plane.GammaSize)
	if len(plane.Formats) > 0 {
		tf.Printf("Formats")
		tfc := tf.NewChild()
		for _, fmt := range plane.Formats {
			tfc.Printf("%v", fmt)
		}
	}
	if len(plane.Properties) > 0 {
		tf.Printf("Properties")
		plane.Properties.FormatTree(tf.NewChild())
	}
}

type Node struct {
	Driver     *Driver     `json:"driver"`
	Device     *Device     `json:"device"`
	Connectors []Connector `json:"connectors"`
	Encoders   []Encoder   `json:"encoders"`
	CRTCs      []CRTC      `json:"crtcs"`
	Planes     []Plane     `json:"planes"`
}

func (n *Node) FormatTree(tf treefmt.Formatter) {
	n.Device.FormatTree(tf)
	n.Driver.FormatTree(tf)

	tf.Printf("Connectors")
	for i, conn := range n.Connectors {
		tfc := tf.NewChild()
		tfc.Printf("Connector %v", i)
		conn.FormatTree(tfc.NewChild())
	}

	tf.Printf("Encoders")
	for i, enc := range n.Encoders {
		tfc := tf.NewChild()
		tfc.Printf("Encoder %v", i)
		enc.FormatTree(tfc.NewChild())
	}

	tf.Printf("CRTCs")
	for i, crtc := range n.CRTCs {
		tfc := tf.NewChild()
		tfc.Printf("CRTC %v", i)
		crtc.FormatTree(tfc.NewChild())
	}

	tf.Printf("Planes")
	for i, plane := range n.Planes {
		tfc := tf.NewChild()
		tfc.Printf("Plane %v", i)
		plane.FormatTree(tfc.NewChild())
	}
}

type NodeMap map[string]*Node

func (m NodeMap) FormatTree(tf treefmt.Formatter) {
	for path, n := range m {
		tf.Printf("Node: %s", path)
		n.FormatTree(tf.NewChild())
	}
}