1*e8c08688SThomas Zimmermann // SPDX-License-Identifier: GPL-2.0-only 2*e8c08688SThomas Zimmermann 3*e8c08688SThomas Zimmermann #include <linux/export.h> 4*e8c08688SThomas Zimmermann #include <linux/limits.h> 5*e8c08688SThomas Zimmermann #include <linux/minmax.h> 6*e8c08688SThomas Zimmermann #include <linux/screen_info.h> 7*e8c08688SThomas Zimmermann 8*e8c08688SThomas Zimmermann #include <drm/drm_fourcc.h> 9*e8c08688SThomas Zimmermann #include <drm/drm_print.h> 10*e8c08688SThomas Zimmermann 11*e8c08688SThomas Zimmermann #include "drm_sysfb_helper.h" 12*e8c08688SThomas Zimmermann 13*e8c08688SThomas Zimmermann static s64 drm_sysfb_get_validated_size0(struct drm_device *dev, const char *name, 14*e8c08688SThomas Zimmermann u64 value, u64 max) 15*e8c08688SThomas Zimmermann { 16*e8c08688SThomas Zimmermann if (!value) { 17*e8c08688SThomas Zimmermann drm_warn(dev, "%s of 0 not allowed\n", name); 18*e8c08688SThomas Zimmermann return -EINVAL; 19*e8c08688SThomas Zimmermann } else if (value > min(max, S64_MAX)) { 20*e8c08688SThomas Zimmermann drm_warn(dev, "%s of %llu exceeds maximum of %llu\n", name, value, max); 21*e8c08688SThomas Zimmermann return -EINVAL; 22*e8c08688SThomas Zimmermann } 23*e8c08688SThomas Zimmermann return value; 24*e8c08688SThomas Zimmermann } 25*e8c08688SThomas Zimmermann 26*e8c08688SThomas Zimmermann int drm_sysfb_get_width_si(struct drm_device *dev, const struct screen_info *si) 27*e8c08688SThomas Zimmermann { 28*e8c08688SThomas Zimmermann return drm_sysfb_get_validated_int0(dev, "width", si->lfb_width, U16_MAX); 29*e8c08688SThomas Zimmermann } 30*e8c08688SThomas Zimmermann EXPORT_SYMBOL(drm_sysfb_get_width_si); 31*e8c08688SThomas Zimmermann 32*e8c08688SThomas Zimmermann int drm_sysfb_get_height_si(struct drm_device *dev, const struct screen_info *si) 33*e8c08688SThomas Zimmermann { 34*e8c08688SThomas Zimmermann return drm_sysfb_get_validated_int0(dev, "height", si->lfb_height, U16_MAX); 35*e8c08688SThomas Zimmermann } 36*e8c08688SThomas Zimmermann EXPORT_SYMBOL(drm_sysfb_get_height_si); 37*e8c08688SThomas Zimmermann 38*e8c08688SThomas Zimmermann struct resource *drm_sysfb_get_memory_si(struct drm_device *dev, 39*e8c08688SThomas Zimmermann const struct screen_info *si, 40*e8c08688SThomas Zimmermann struct resource *res) 41*e8c08688SThomas Zimmermann { 42*e8c08688SThomas Zimmermann ssize_t num; 43*e8c08688SThomas Zimmermann 44*e8c08688SThomas Zimmermann num = screen_info_resources(si, res, 1); 45*e8c08688SThomas Zimmermann if (!num) { 46*e8c08688SThomas Zimmermann drm_warn(dev, "memory resource not found\n"); 47*e8c08688SThomas Zimmermann return NULL; 48*e8c08688SThomas Zimmermann } 49*e8c08688SThomas Zimmermann 50*e8c08688SThomas Zimmermann return res; 51*e8c08688SThomas Zimmermann } 52*e8c08688SThomas Zimmermann EXPORT_SYMBOL(drm_sysfb_get_memory_si); 53*e8c08688SThomas Zimmermann 54*e8c08688SThomas Zimmermann int drm_sysfb_get_stride_si(struct drm_device *dev, const struct screen_info *si, 55*e8c08688SThomas Zimmermann const struct drm_format_info *format, 56*e8c08688SThomas Zimmermann unsigned int width, unsigned int height, u64 size) 57*e8c08688SThomas Zimmermann { 58*e8c08688SThomas Zimmermann u64 lfb_linelength = si->lfb_linelength; 59*e8c08688SThomas Zimmermann 60*e8c08688SThomas Zimmermann if (!lfb_linelength) 61*e8c08688SThomas Zimmermann lfb_linelength = drm_format_info_min_pitch(format, 0, width); 62*e8c08688SThomas Zimmermann 63*e8c08688SThomas Zimmermann return drm_sysfb_get_validated_int0(dev, "stride", lfb_linelength, div64_u64(size, height)); 64*e8c08688SThomas Zimmermann } 65*e8c08688SThomas Zimmermann EXPORT_SYMBOL(drm_sysfb_get_stride_si); 66*e8c08688SThomas Zimmermann 67*e8c08688SThomas Zimmermann u64 drm_sysfb_get_visible_size_si(struct drm_device *dev, const struct screen_info *si, 68*e8c08688SThomas Zimmermann unsigned int height, unsigned int stride, u64 size) 69*e8c08688SThomas Zimmermann { 70*e8c08688SThomas Zimmermann u64 vsize = PAGE_ALIGN(height * stride); 71*e8c08688SThomas Zimmermann 72*e8c08688SThomas Zimmermann return drm_sysfb_get_validated_size0(dev, "visible size", vsize, size); 73*e8c08688SThomas Zimmermann } 74*e8c08688SThomas Zimmermann EXPORT_SYMBOL(drm_sysfb_get_visible_size_si); 75*e8c08688SThomas Zimmermann 76*e8c08688SThomas Zimmermann const struct drm_format_info *drm_sysfb_get_format_si(struct drm_device *dev, 77*e8c08688SThomas Zimmermann const struct drm_sysfb_format *formats, 78*e8c08688SThomas Zimmermann size_t nformats, 79*e8c08688SThomas Zimmermann const struct screen_info *si) 80*e8c08688SThomas Zimmermann { 81*e8c08688SThomas Zimmermann const struct drm_format_info *format = NULL; 82*e8c08688SThomas Zimmermann u32 bits_per_pixel; 83*e8c08688SThomas Zimmermann size_t i; 84*e8c08688SThomas Zimmermann 85*e8c08688SThomas Zimmermann bits_per_pixel = __screen_info_lfb_bits_per_pixel(si); 86*e8c08688SThomas Zimmermann 87*e8c08688SThomas Zimmermann for (i = 0; i < nformats; ++i) { 88*e8c08688SThomas Zimmermann const struct pixel_format *f = &formats[i].pixel; 89*e8c08688SThomas Zimmermann 90*e8c08688SThomas Zimmermann if (bits_per_pixel == f->bits_per_pixel && 91*e8c08688SThomas Zimmermann si->red_size == f->red.length && 92*e8c08688SThomas Zimmermann si->red_pos == f->red.offset && 93*e8c08688SThomas Zimmermann si->green_size == f->green.length && 94*e8c08688SThomas Zimmermann si->green_pos == f->green.offset && 95*e8c08688SThomas Zimmermann si->blue_size == f->blue.length && 96*e8c08688SThomas Zimmermann si->blue_pos == f->blue.offset) { 97*e8c08688SThomas Zimmermann format = drm_format_info(formats[i].fourcc); 98*e8c08688SThomas Zimmermann break; 99*e8c08688SThomas Zimmermann } 100*e8c08688SThomas Zimmermann } 101*e8c08688SThomas Zimmermann 102*e8c08688SThomas Zimmermann if (!format) 103*e8c08688SThomas Zimmermann drm_warn(dev, "No compatible color format found\n"); 104*e8c08688SThomas Zimmermann 105*e8c08688SThomas Zimmermann return format; 106*e8c08688SThomas Zimmermann } 107*e8c08688SThomas Zimmermann EXPORT_SYMBOL(drm_sysfb_get_format_si); 108