drm_crtc.c 148 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536
  1. /*
  2. * Copyright (c) 2006-2008 Intel Corporation
  3. * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
  4. * Copyright (c) 2008 Red Hat Inc.
  5. *
  6. * DRM core CRTC related functions
  7. *
  8. * Permission to use, copy, modify, distribute, and sell this software and its
  9. * documentation for any purpose is hereby granted without fee, provided that
  10. * the above copyright notice appear in all copies and that both that copyright
  11. * notice and this permission notice appear in supporting documentation, and
  12. * that the name of the copyright holders not be used in advertising or
  13. * publicity pertaining to distribution of the software without specific,
  14. * written prior permission. The copyright holders make no representations
  15. * about the suitability of this software for any purpose. It is provided "as
  16. * is" without express or implied warranty.
  17. *
  18. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  19. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  20. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  21. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  22. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  23. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  24. * OF THIS SOFTWARE.
  25. *
  26. * Authors:
  27. * Keith Packard
  28. * Eric Anholt <eric@anholt.net>
  29. * Dave Airlie <airlied@linux.ie>
  30. * Jesse Barnes <jesse.barnes@intel.com>
  31. */
  32. #include <linux/ctype.h>
  33. #include <linux/list.h>
  34. #include <linux/slab.h>
  35. #include <linux/export.h>
  36. #include <drm/drmP.h>
  37. #include <drm/drm_crtc.h>
  38. #include <drm/drm_edid.h>
  39. #include <drm/drm_fourcc.h>
  40. #include <drm/drm_modeset_lock.h>
  41. #include <drm/drm_atomic.h>
  42. #include "drm_crtc_internal.h"
  43. #include "drm_internal.h"
  44. static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
  45. struct drm_mode_fb_cmd2 *r,
  46. struct drm_file *file_priv);
  47. /* Avoid boilerplate. I'm tired of typing. */
  48. #define DRM_ENUM_NAME_FN(fnname, list) \
  49. const char *fnname(int val) \
  50. { \
  51. int i; \
  52. for (i = 0; i < ARRAY_SIZE(list); i++) { \
  53. if (list[i].type == val) \
  54. return list[i].name; \
  55. } \
  56. return "(unknown)"; \
  57. }
  58. /*
  59. * Global properties
  60. */
  61. static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
  62. { DRM_MODE_DPMS_ON, "On" },
  63. { DRM_MODE_DPMS_STANDBY, "Standby" },
  64. { DRM_MODE_DPMS_SUSPEND, "Suspend" },
  65. { DRM_MODE_DPMS_OFF, "Off" }
  66. };
  67. DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
  68. static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
  69. { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
  70. { DRM_PLANE_TYPE_PRIMARY, "Primary" },
  71. { DRM_PLANE_TYPE_CURSOR, "Cursor" },
  72. };
  73. /*
  74. * Optional properties
  75. */
  76. static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
  77. { DRM_MODE_SCALE_NONE, "None" },
  78. { DRM_MODE_SCALE_FULLSCREEN, "Full" },
  79. { DRM_MODE_SCALE_CENTER, "Center" },
  80. { DRM_MODE_SCALE_ASPECT, "Full aspect" },
  81. };
  82. static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
  83. { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
  84. { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
  85. { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
  86. };
  87. /*
  88. * Non-global properties, but "required" for certain connectors.
  89. */
  90. static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
  91. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  92. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  93. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  94. };
  95. DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
  96. static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
  97. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  98. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  99. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  100. };
  101. DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
  102. drm_dvi_i_subconnector_enum_list)
  103. static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
  104. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  105. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  106. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  107. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  108. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  109. };
  110. DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
  111. static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
  112. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  113. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  114. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  115. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  116. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  117. };
  118. DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
  119. drm_tv_subconnector_enum_list)
  120. static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
  121. { DRM_MODE_DIRTY_OFF, "Off" },
  122. { DRM_MODE_DIRTY_ON, "On" },
  123. { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
  124. };
  125. struct drm_conn_prop_enum_list {
  126. int type;
  127. const char *name;
  128. struct ida ida;
  129. };
  130. /*
  131. * Connector and encoder types.
  132. */
  133. static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
  134. { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
  135. { DRM_MODE_CONNECTOR_VGA, "VGA" },
  136. { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
  137. { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
  138. { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
  139. { DRM_MODE_CONNECTOR_Composite, "Composite" },
  140. { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
  141. { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
  142. { DRM_MODE_CONNECTOR_Component, "Component" },
  143. { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
  144. { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
  145. { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
  146. { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
  147. { DRM_MODE_CONNECTOR_TV, "TV" },
  148. { DRM_MODE_CONNECTOR_eDP, "eDP" },
  149. { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
  150. { DRM_MODE_CONNECTOR_DSI, "DSI" },
  151. };
  152. static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
  153. { DRM_MODE_ENCODER_NONE, "None" },
  154. { DRM_MODE_ENCODER_DAC, "DAC" },
  155. { DRM_MODE_ENCODER_TMDS, "TMDS" },
  156. { DRM_MODE_ENCODER_LVDS, "LVDS" },
  157. { DRM_MODE_ENCODER_TVDAC, "TV" },
  158. { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
  159. { DRM_MODE_ENCODER_DSI, "DSI" },
  160. { DRM_MODE_ENCODER_DPMST, "DP MST" },
  161. };
  162. static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
  163. { SubPixelUnknown, "Unknown" },
  164. { SubPixelHorizontalRGB, "Horizontal RGB" },
  165. { SubPixelHorizontalBGR, "Horizontal BGR" },
  166. { SubPixelVerticalRGB, "Vertical RGB" },
  167. { SubPixelVerticalBGR, "Vertical BGR" },
  168. { SubPixelNone, "None" },
  169. };
  170. void drm_connector_ida_init(void)
  171. {
  172. int i;
  173. for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  174. ida_init(&drm_connector_enum_list[i].ida);
  175. }
  176. void drm_connector_ida_destroy(void)
  177. {
  178. int i;
  179. for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  180. ida_destroy(&drm_connector_enum_list[i].ida);
  181. }
  182. /**
  183. * drm_get_connector_status_name - return a string for connector status
  184. * @status: connector status to compute name of
  185. *
  186. * In contrast to the other drm_get_*_name functions this one here returns a
  187. * const pointer and hence is threadsafe.
  188. */
  189. const char *drm_get_connector_status_name(enum drm_connector_status status)
  190. {
  191. if (status == connector_status_connected)
  192. return "connected";
  193. else if (status == connector_status_disconnected)
  194. return "disconnected";
  195. else
  196. return "unknown";
  197. }
  198. EXPORT_SYMBOL(drm_get_connector_status_name);
  199. /**
  200. * drm_get_subpixel_order_name - return a string for a given subpixel enum
  201. * @order: enum of subpixel_order
  202. *
  203. * Note you could abuse this and return something out of bounds, but that
  204. * would be a caller error. No unscrubbed user data should make it here.
  205. */
  206. const char *drm_get_subpixel_order_name(enum subpixel_order order)
  207. {
  208. return drm_subpixel_enum_list[order].name;
  209. }
  210. EXPORT_SYMBOL(drm_get_subpixel_order_name);
  211. static char printable_char(int c)
  212. {
  213. return isascii(c) && isprint(c) ? c : '?';
  214. }
  215. /**
  216. * drm_get_format_name - return a string for drm fourcc format
  217. * @format: format to compute name of
  218. *
  219. * Note that the buffer used by this function is globally shared and owned by
  220. * the function itself.
  221. *
  222. * FIXME: This isn't really multithreading safe.
  223. */
  224. const char *drm_get_format_name(uint32_t format)
  225. {
  226. static char buf[32];
  227. snprintf(buf, sizeof(buf),
  228. "%c%c%c%c %s-endian (0x%08x)",
  229. printable_char(format & 0xff),
  230. printable_char((format >> 8) & 0xff),
  231. printable_char((format >> 16) & 0xff),
  232. printable_char((format >> 24) & 0x7f),
  233. format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
  234. format);
  235. return buf;
  236. }
  237. EXPORT_SYMBOL(drm_get_format_name);
  238. /*
  239. * Internal function to assign a slot in the object idr and optionally
  240. * register the object into the idr.
  241. */
  242. static int drm_mode_object_get_reg(struct drm_device *dev,
  243. struct drm_mode_object *obj,
  244. uint32_t obj_type,
  245. bool register_obj)
  246. {
  247. int ret;
  248. mutex_lock(&dev->mode_config.idr_mutex);
  249. ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
  250. if (ret >= 0) {
  251. /*
  252. * Set up the object linking under the protection of the idr
  253. * lock so that other users can't see inconsistent state.
  254. */
  255. obj->id = ret;
  256. obj->type = obj_type;
  257. }
  258. mutex_unlock(&dev->mode_config.idr_mutex);
  259. return ret < 0 ? ret : 0;
  260. }
  261. /**
  262. * drm_mode_object_get - allocate a new modeset identifier
  263. * @dev: DRM device
  264. * @obj: object pointer, used to generate unique ID
  265. * @obj_type: object type
  266. *
  267. * Create a unique identifier based on @ptr in @dev's identifier space. Used
  268. * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
  269. * modeset identifiers are _not_ reference counted. Hence don't use this for
  270. * reference counted modeset objects like framebuffers.
  271. *
  272. * Returns:
  273. * New unique (relative to other objects in @dev) integer identifier for the
  274. * object.
  275. */
  276. int drm_mode_object_get(struct drm_device *dev,
  277. struct drm_mode_object *obj, uint32_t obj_type)
  278. {
  279. return drm_mode_object_get_reg(dev, obj, obj_type, true);
  280. }
  281. static void drm_mode_object_register(struct drm_device *dev,
  282. struct drm_mode_object *obj)
  283. {
  284. mutex_lock(&dev->mode_config.idr_mutex);
  285. idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
  286. mutex_unlock(&dev->mode_config.idr_mutex);
  287. }
  288. /**
  289. * drm_mode_object_put - free a modeset identifer
  290. * @dev: DRM device
  291. * @object: object to free
  292. *
  293. * Free @id from @dev's unique identifier pool. Note that despite the _get
  294. * postfix modeset identifiers are _not_ reference counted. Hence don't use this
  295. * for reference counted modeset objects like framebuffers.
  296. */
  297. void drm_mode_object_put(struct drm_device *dev,
  298. struct drm_mode_object *object)
  299. {
  300. mutex_lock(&dev->mode_config.idr_mutex);
  301. idr_remove(&dev->mode_config.crtc_idr, object->id);
  302. mutex_unlock(&dev->mode_config.idr_mutex);
  303. }
  304. static struct drm_mode_object *_object_find(struct drm_device *dev,
  305. uint32_t id, uint32_t type)
  306. {
  307. struct drm_mode_object *obj = NULL;
  308. mutex_lock(&dev->mode_config.idr_mutex);
  309. obj = idr_find(&dev->mode_config.crtc_idr, id);
  310. if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
  311. obj = NULL;
  312. if (obj && obj->id != id)
  313. obj = NULL;
  314. /* don't leak out unref'd fb's */
  315. if (obj && (obj->type == DRM_MODE_OBJECT_FB))
  316. obj = NULL;
  317. mutex_unlock(&dev->mode_config.idr_mutex);
  318. return obj;
  319. }
  320. /**
  321. * drm_mode_object_find - look up a drm object with static lifetime
  322. * @dev: drm device
  323. * @id: id of the mode object
  324. * @type: type of the mode object
  325. *
  326. * Note that framebuffers cannot be looked up with this functions - since those
  327. * are reference counted, they need special treatment. Even with
  328. * DRM_MODE_OBJECT_ANY (although that will simply return NULL
  329. * rather than WARN_ON()).
  330. */
  331. struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
  332. uint32_t id, uint32_t type)
  333. {
  334. struct drm_mode_object *obj = NULL;
  335. /* Framebuffers are reference counted and need their own lookup
  336. * function.*/
  337. WARN_ON(type == DRM_MODE_OBJECT_FB);
  338. obj = _object_find(dev, id, type);
  339. return obj;
  340. }
  341. EXPORT_SYMBOL(drm_mode_object_find);
  342. /**
  343. * drm_framebuffer_init - initialize a framebuffer
  344. * @dev: DRM device
  345. * @fb: framebuffer to be initialized
  346. * @funcs: ... with these functions
  347. *
  348. * Allocates an ID for the framebuffer's parent mode object, sets its mode
  349. * functions & device file and adds it to the master fd list.
  350. *
  351. * IMPORTANT:
  352. * This functions publishes the fb and makes it available for concurrent access
  353. * by other users. Which means by this point the fb _must_ be fully set up -
  354. * since all the fb attributes are invariant over its lifetime, no further
  355. * locking but only correct reference counting is required.
  356. *
  357. * Returns:
  358. * Zero on success, error code on failure.
  359. */
  360. int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
  361. const struct drm_framebuffer_funcs *funcs)
  362. {
  363. int ret;
  364. mutex_lock(&dev->mode_config.fb_lock);
  365. kref_init(&fb->refcount);
  366. INIT_LIST_HEAD(&fb->filp_head);
  367. fb->dev = dev;
  368. fb->funcs = funcs;
  369. ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
  370. if (ret)
  371. goto out;
  372. dev->mode_config.num_fb++;
  373. list_add(&fb->head, &dev->mode_config.fb_list);
  374. out:
  375. mutex_unlock(&dev->mode_config.fb_lock);
  376. return 0;
  377. }
  378. EXPORT_SYMBOL(drm_framebuffer_init);
  379. /* dev->mode_config.fb_lock must be held! */
  380. static void __drm_framebuffer_unregister(struct drm_device *dev,
  381. struct drm_framebuffer *fb)
  382. {
  383. mutex_lock(&dev->mode_config.idr_mutex);
  384. idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
  385. mutex_unlock(&dev->mode_config.idr_mutex);
  386. fb->base.id = 0;
  387. }
  388. static void drm_framebuffer_free(struct kref *kref)
  389. {
  390. struct drm_framebuffer *fb =
  391. container_of(kref, struct drm_framebuffer, refcount);
  392. struct drm_device *dev = fb->dev;
  393. /*
  394. * The lookup idr holds a weak reference, which has not necessarily been
  395. * removed at this point. Check for that.
  396. */
  397. mutex_lock(&dev->mode_config.fb_lock);
  398. if (fb->base.id) {
  399. /* Mark fb as reaped and drop idr ref. */
  400. __drm_framebuffer_unregister(dev, fb);
  401. }
  402. mutex_unlock(&dev->mode_config.fb_lock);
  403. fb->funcs->destroy(fb);
  404. }
  405. static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
  406. uint32_t id)
  407. {
  408. struct drm_mode_object *obj = NULL;
  409. struct drm_framebuffer *fb;
  410. mutex_lock(&dev->mode_config.idr_mutex);
  411. obj = idr_find(&dev->mode_config.crtc_idr, id);
  412. if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
  413. fb = NULL;
  414. else
  415. fb = obj_to_fb(obj);
  416. mutex_unlock(&dev->mode_config.idr_mutex);
  417. return fb;
  418. }
  419. /**
  420. * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
  421. * @dev: drm device
  422. * @id: id of the fb object
  423. *
  424. * If successful, this grabs an additional reference to the framebuffer -
  425. * callers need to make sure to eventually unreference the returned framebuffer
  426. * again, using @drm_framebuffer_unreference.
  427. */
  428. struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
  429. uint32_t id)
  430. {
  431. struct drm_framebuffer *fb;
  432. mutex_lock(&dev->mode_config.fb_lock);
  433. fb = __drm_framebuffer_lookup(dev, id);
  434. if (fb) {
  435. if (!kref_get_unless_zero(&fb->refcount))
  436. fb = NULL;
  437. }
  438. mutex_unlock(&dev->mode_config.fb_lock);
  439. return fb;
  440. }
  441. EXPORT_SYMBOL(drm_framebuffer_lookup);
  442. /**
  443. * drm_framebuffer_unreference - unref a framebuffer
  444. * @fb: framebuffer to unref
  445. *
  446. * This functions decrements the fb's refcount and frees it if it drops to zero.
  447. */
  448. void drm_framebuffer_unreference(struct drm_framebuffer *fb)
  449. {
  450. DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
  451. kref_put(&fb->refcount, drm_framebuffer_free);
  452. }
  453. EXPORT_SYMBOL(drm_framebuffer_unreference);
  454. /**
  455. * drm_framebuffer_reference - incr the fb refcnt
  456. * @fb: framebuffer
  457. *
  458. * This functions increments the fb's refcount.
  459. */
  460. void drm_framebuffer_reference(struct drm_framebuffer *fb)
  461. {
  462. DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
  463. kref_get(&fb->refcount);
  464. }
  465. EXPORT_SYMBOL(drm_framebuffer_reference);
  466. static void drm_framebuffer_free_bug(struct kref *kref)
  467. {
  468. BUG();
  469. }
  470. static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
  471. {
  472. DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
  473. kref_put(&fb->refcount, drm_framebuffer_free_bug);
  474. }
  475. /**
  476. * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
  477. * @fb: fb to unregister
  478. *
  479. * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
  480. * those used for fbdev. Note that the caller must hold a reference of it's own,
  481. * i.e. the object may not be destroyed through this call (since it'll lead to a
  482. * locking inversion).
  483. */
  484. void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
  485. {
  486. struct drm_device *dev = fb->dev;
  487. mutex_lock(&dev->mode_config.fb_lock);
  488. /* Mark fb as reaped and drop idr ref. */
  489. __drm_framebuffer_unregister(dev, fb);
  490. mutex_unlock(&dev->mode_config.fb_lock);
  491. }
  492. EXPORT_SYMBOL(drm_framebuffer_unregister_private);
  493. /**
  494. * drm_framebuffer_cleanup - remove a framebuffer object
  495. * @fb: framebuffer to remove
  496. *
  497. * Cleanup framebuffer. This function is intended to be used from the drivers
  498. * ->destroy callback. It can also be used to clean up driver private
  499. * framebuffers embedded into a larger structure.
  500. *
  501. * Note that this function does not remove the fb from active usuage - if it is
  502. * still used anywhere, hilarity can ensue since userspace could call getfb on
  503. * the id and get back -EINVAL. Obviously no concern at driver unload time.
  504. *
  505. * Also, the framebuffer will not be removed from the lookup idr - for
  506. * user-created framebuffers this will happen in in the rmfb ioctl. For
  507. * driver-private objects (e.g. for fbdev) drivers need to explicitly call
  508. * drm_framebuffer_unregister_private.
  509. */
  510. void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
  511. {
  512. struct drm_device *dev = fb->dev;
  513. mutex_lock(&dev->mode_config.fb_lock);
  514. list_del(&fb->head);
  515. dev->mode_config.num_fb--;
  516. mutex_unlock(&dev->mode_config.fb_lock);
  517. }
  518. EXPORT_SYMBOL(drm_framebuffer_cleanup);
  519. /**
  520. * drm_framebuffer_remove - remove and unreference a framebuffer object
  521. * @fb: framebuffer to remove
  522. *
  523. * Scans all the CRTCs and planes in @dev's mode_config. If they're
  524. * using @fb, removes it, setting it to NULL. Then drops the reference to the
  525. * passed-in framebuffer. Might take the modeset locks.
  526. *
  527. * Note that this function optimizes the cleanup away if the caller holds the
  528. * last reference to the framebuffer. It is also guaranteed to not take the
  529. * modeset locks in this case.
  530. */
  531. void drm_framebuffer_remove(struct drm_framebuffer *fb)
  532. {
  533. struct drm_device *dev = fb->dev;
  534. struct drm_crtc *crtc;
  535. struct drm_plane *plane;
  536. struct drm_mode_set set;
  537. int ret;
  538. WARN_ON(!list_empty(&fb->filp_head));
  539. /*
  540. * drm ABI mandates that we remove any deleted framebuffers from active
  541. * useage. But since most sane clients only remove framebuffers they no
  542. * longer need, try to optimize this away.
  543. *
  544. * Since we're holding a reference ourselves, observing a refcount of 1
  545. * means that we're the last holder and can skip it. Also, the refcount
  546. * can never increase from 1 again, so we don't need any barriers or
  547. * locks.
  548. *
  549. * Note that userspace could try to race with use and instate a new
  550. * usage _after_ we've cleared all current ones. End result will be an
  551. * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
  552. * in this manner.
  553. */
  554. if (atomic_read(&fb->refcount.refcount) > 1) {
  555. drm_modeset_lock_all(dev);
  556. /* remove from any CRTC */
  557. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  558. if (crtc->primary->fb == fb) {
  559. /* should turn off the crtc */
  560. memset(&set, 0, sizeof(struct drm_mode_set));
  561. set.crtc = crtc;
  562. set.fb = NULL;
  563. ret = drm_mode_set_config_internal(&set);
  564. if (ret)
  565. DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
  566. }
  567. }
  568. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  569. if (plane->fb == fb)
  570. drm_plane_force_disable(plane);
  571. }
  572. drm_modeset_unlock_all(dev);
  573. }
  574. drm_framebuffer_unreference(fb);
  575. }
  576. EXPORT_SYMBOL(drm_framebuffer_remove);
  577. DEFINE_WW_CLASS(crtc_ww_class);
  578. /**
  579. * drm_crtc_init_with_planes - Initialise a new CRTC object with
  580. * specified primary and cursor planes.
  581. * @dev: DRM device
  582. * @crtc: CRTC object to init
  583. * @primary: Primary plane for CRTC
  584. * @cursor: Cursor plane for CRTC
  585. * @funcs: callbacks for the new CRTC
  586. *
  587. * Inits a new object created as base part of a driver crtc object.
  588. *
  589. * Returns:
  590. * Zero on success, error code on failure.
  591. */
  592. int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
  593. struct drm_plane *primary,
  594. struct drm_plane *cursor,
  595. const struct drm_crtc_funcs *funcs)
  596. {
  597. struct drm_mode_config *config = &dev->mode_config;
  598. int ret;
  599. crtc->dev = dev;
  600. crtc->funcs = funcs;
  601. crtc->invert_dimensions = false;
  602. drm_modeset_lock_init(&crtc->mutex);
  603. ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
  604. if (ret)
  605. return ret;
  606. crtc->base.properties = &crtc->properties;
  607. list_add_tail(&crtc->head, &config->crtc_list);
  608. config->num_crtc++;
  609. crtc->primary = primary;
  610. crtc->cursor = cursor;
  611. if (primary)
  612. primary->possible_crtcs = 1 << drm_crtc_index(crtc);
  613. if (cursor)
  614. cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
  615. return 0;
  616. }
  617. EXPORT_SYMBOL(drm_crtc_init_with_planes);
  618. /**
  619. * drm_crtc_cleanup - Clean up the core crtc usage
  620. * @crtc: CRTC to cleanup
  621. *
  622. * This function cleans up @crtc and removes it from the DRM mode setting
  623. * core. Note that the function does *not* free the crtc structure itself,
  624. * this is the responsibility of the caller.
  625. */
  626. void drm_crtc_cleanup(struct drm_crtc *crtc)
  627. {
  628. struct drm_device *dev = crtc->dev;
  629. kfree(crtc->gamma_store);
  630. crtc->gamma_store = NULL;
  631. drm_modeset_lock_fini(&crtc->mutex);
  632. drm_mode_object_put(dev, &crtc->base);
  633. list_del(&crtc->head);
  634. dev->mode_config.num_crtc--;
  635. WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
  636. if (crtc->state && crtc->funcs->atomic_destroy_state)
  637. crtc->funcs->atomic_destroy_state(crtc, crtc->state);
  638. memset(crtc, 0, sizeof(*crtc));
  639. }
  640. EXPORT_SYMBOL(drm_crtc_cleanup);
  641. /**
  642. * drm_crtc_index - find the index of a registered CRTC
  643. * @crtc: CRTC to find index for
  644. *
  645. * Given a registered CRTC, return the index of that CRTC within a DRM
  646. * device's list of CRTCs.
  647. */
  648. unsigned int drm_crtc_index(struct drm_crtc *crtc)
  649. {
  650. unsigned int index = 0;
  651. struct drm_crtc *tmp;
  652. list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
  653. if (tmp == crtc)
  654. return index;
  655. index++;
  656. }
  657. BUG();
  658. }
  659. EXPORT_SYMBOL(drm_crtc_index);
  660. /*
  661. * drm_mode_remove - remove and free a mode
  662. * @connector: connector list to modify
  663. * @mode: mode to remove
  664. *
  665. * Remove @mode from @connector's mode list, then free it.
  666. */
  667. static void drm_mode_remove(struct drm_connector *connector,
  668. struct drm_display_mode *mode)
  669. {
  670. list_del(&mode->head);
  671. drm_mode_destroy(connector->dev, mode);
  672. }
  673. /**
  674. * drm_connector_get_cmdline_mode - reads the user's cmdline mode
  675. * @connector: connector to quwery
  676. *
  677. * The kernel supports per-connector configration of its consoles through
  678. * use of the video= parameter. This function parses that option and
  679. * extracts the user's specified mode (or enable/disable status) for a
  680. * particular connector. This is typically only used during the early fbdev
  681. * setup.
  682. */
  683. static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
  684. {
  685. struct drm_cmdline_mode *mode = &connector->cmdline_mode;
  686. char *option = NULL;
  687. if (fb_get_options(connector->name, &option))
  688. return;
  689. if (!drm_mode_parse_command_line_for_connector(option,
  690. connector,
  691. mode))
  692. return;
  693. if (mode->force) {
  694. const char *s;
  695. switch (mode->force) {
  696. case DRM_FORCE_OFF:
  697. s = "OFF";
  698. break;
  699. case DRM_FORCE_ON_DIGITAL:
  700. s = "ON - dig";
  701. break;
  702. default:
  703. case DRM_FORCE_ON:
  704. s = "ON";
  705. break;
  706. }
  707. DRM_INFO("forcing %s connector %s\n", connector->name, s);
  708. connector->force = mode->force;
  709. }
  710. DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
  711. connector->name,
  712. mode->xres, mode->yres,
  713. mode->refresh_specified ? mode->refresh : 60,
  714. mode->rb ? " reduced blanking" : "",
  715. mode->margins ? " with margins" : "",
  716. mode->interlace ? " interlaced" : "");
  717. }
  718. /**
  719. * drm_connector_init - Init a preallocated connector
  720. * @dev: DRM device
  721. * @connector: the connector to init
  722. * @funcs: callbacks for this connector
  723. * @connector_type: user visible type of the connector
  724. *
  725. * Initialises a preallocated connector. Connectors should be
  726. * subclassed as part of driver connector objects.
  727. *
  728. * Returns:
  729. * Zero on success, error code on failure.
  730. */
  731. int drm_connector_init(struct drm_device *dev,
  732. struct drm_connector *connector,
  733. const struct drm_connector_funcs *funcs,
  734. int connector_type)
  735. {
  736. int ret;
  737. struct ida *connector_ida =
  738. &drm_connector_enum_list[connector_type].ida;
  739. drm_modeset_lock_all(dev);
  740. ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
  741. if (ret)
  742. goto out_unlock;
  743. connector->base.properties = &connector->properties;
  744. connector->dev = dev;
  745. connector->funcs = funcs;
  746. connector->connector_type = connector_type;
  747. connector->connector_type_id =
  748. ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
  749. if (connector->connector_type_id < 0) {
  750. ret = connector->connector_type_id;
  751. goto out_put;
  752. }
  753. connector->name =
  754. kasprintf(GFP_KERNEL, "%s-%d",
  755. drm_connector_enum_list[connector_type].name,
  756. connector->connector_type_id);
  757. if (!connector->name) {
  758. ret = -ENOMEM;
  759. goto out_put;
  760. }
  761. INIT_LIST_HEAD(&connector->probed_modes);
  762. INIT_LIST_HEAD(&connector->modes);
  763. connector->edid_blob_ptr = NULL;
  764. connector->status = connector_status_unknown;
  765. drm_connector_get_cmdline_mode(connector);
  766. /* We should add connectors at the end to avoid upsetting the connector
  767. * index too much. */
  768. list_add_tail(&connector->head, &dev->mode_config.connector_list);
  769. dev->mode_config.num_connector++;
  770. if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
  771. drm_object_attach_property(&connector->base,
  772. dev->mode_config.edid_property,
  773. 0);
  774. drm_object_attach_property(&connector->base,
  775. dev->mode_config.dpms_property, 0);
  776. connector->debugfs_entry = NULL;
  777. out_put:
  778. if (ret)
  779. drm_mode_object_put(dev, &connector->base);
  780. out_unlock:
  781. drm_modeset_unlock_all(dev);
  782. return ret;
  783. }
  784. EXPORT_SYMBOL(drm_connector_init);
  785. /**
  786. * drm_connector_cleanup - cleans up an initialised connector
  787. * @connector: connector to cleanup
  788. *
  789. * Cleans up the connector but doesn't free the object.
  790. */
  791. void drm_connector_cleanup(struct drm_connector *connector)
  792. {
  793. struct drm_device *dev = connector->dev;
  794. struct drm_display_mode *mode, *t;
  795. if (connector->tile_group) {
  796. drm_mode_put_tile_group(dev, connector->tile_group);
  797. connector->tile_group = NULL;
  798. }
  799. list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
  800. drm_mode_remove(connector, mode);
  801. list_for_each_entry_safe(mode, t, &connector->modes, head)
  802. drm_mode_remove(connector, mode);
  803. ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
  804. connector->connector_type_id);
  805. drm_mode_object_put(dev, &connector->base);
  806. kfree(connector->name);
  807. connector->name = NULL;
  808. list_del(&connector->head);
  809. dev->mode_config.num_connector--;
  810. WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
  811. if (connector->state && connector->funcs->atomic_destroy_state)
  812. connector->funcs->atomic_destroy_state(connector,
  813. connector->state);
  814. memset(connector, 0, sizeof(*connector));
  815. }
  816. EXPORT_SYMBOL(drm_connector_cleanup);
  817. /**
  818. * drm_connector_index - find the index of a registered connector
  819. * @connector: connector to find index for
  820. *
  821. * Given a registered connector, return the index of that connector within a DRM
  822. * device's list of connectors.
  823. */
  824. unsigned int drm_connector_index(struct drm_connector *connector)
  825. {
  826. unsigned int index = 0;
  827. struct drm_connector *tmp;
  828. struct drm_mode_config *config = &connector->dev->mode_config;
  829. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  830. list_for_each_entry(tmp, &connector->dev->mode_config.connector_list, head) {
  831. if (tmp == connector)
  832. return index;
  833. index++;
  834. }
  835. BUG();
  836. }
  837. EXPORT_SYMBOL(drm_connector_index);
  838. /**
  839. * drm_connector_register - register a connector
  840. * @connector: the connector to register
  841. *
  842. * Register userspace interfaces for a connector
  843. *
  844. * Returns:
  845. * Zero on success, error code on failure.
  846. */
  847. int drm_connector_register(struct drm_connector *connector)
  848. {
  849. int ret;
  850. drm_mode_object_register(connector->dev, &connector->base);
  851. ret = drm_sysfs_connector_add(connector);
  852. if (ret)
  853. return ret;
  854. ret = drm_debugfs_connector_add(connector);
  855. if (ret) {
  856. drm_sysfs_connector_remove(connector);
  857. return ret;
  858. }
  859. return 0;
  860. }
  861. EXPORT_SYMBOL(drm_connector_register);
  862. /**
  863. * drm_connector_unregister - unregister a connector
  864. * @connector: the connector to unregister
  865. *
  866. * Unregister userspace interfaces for a connector
  867. */
  868. void drm_connector_unregister(struct drm_connector *connector)
  869. {
  870. drm_sysfs_connector_remove(connector);
  871. drm_debugfs_connector_remove(connector);
  872. }
  873. EXPORT_SYMBOL(drm_connector_unregister);
  874. /**
  875. * drm_connector_unplug_all - unregister connector userspace interfaces
  876. * @dev: drm device
  877. *
  878. * This function unregisters all connector userspace interfaces in sysfs. Should
  879. * be call when the device is disconnected, e.g. from an usb driver's
  880. * ->disconnect callback.
  881. */
  882. void drm_connector_unplug_all(struct drm_device *dev)
  883. {
  884. struct drm_connector *connector;
  885. /* taking the mode config mutex ends up in a clash with sysfs */
  886. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  887. drm_connector_unregister(connector);
  888. }
  889. EXPORT_SYMBOL(drm_connector_unplug_all);
  890. /**
  891. * drm_bridge_init - initialize a drm transcoder/bridge
  892. * @dev: drm device
  893. * @bridge: transcoder/bridge to set up
  894. * @funcs: bridge function table
  895. *
  896. * Initialises a preallocated bridge. Bridges should be
  897. * subclassed as part of driver connector objects.
  898. *
  899. * Returns:
  900. * Zero on success, error code on failure.
  901. */
  902. int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
  903. const struct drm_bridge_funcs *funcs)
  904. {
  905. int ret;
  906. drm_modeset_lock_all(dev);
  907. ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
  908. if (ret)
  909. goto out;
  910. bridge->dev = dev;
  911. bridge->funcs = funcs;
  912. list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
  913. dev->mode_config.num_bridge++;
  914. out:
  915. drm_modeset_unlock_all(dev);
  916. return ret;
  917. }
  918. EXPORT_SYMBOL(drm_bridge_init);
  919. /**
  920. * drm_bridge_cleanup - cleans up an initialised bridge
  921. * @bridge: bridge to cleanup
  922. *
  923. * Cleans up the bridge but doesn't free the object.
  924. */
  925. void drm_bridge_cleanup(struct drm_bridge *bridge)
  926. {
  927. struct drm_device *dev = bridge->dev;
  928. drm_modeset_lock_all(dev);
  929. drm_mode_object_put(dev, &bridge->base);
  930. list_del(&bridge->head);
  931. dev->mode_config.num_bridge--;
  932. drm_modeset_unlock_all(dev);
  933. memset(bridge, 0, sizeof(*bridge));
  934. }
  935. EXPORT_SYMBOL(drm_bridge_cleanup);
  936. /**
  937. * drm_encoder_init - Init a preallocated encoder
  938. * @dev: drm device
  939. * @encoder: the encoder to init
  940. * @funcs: callbacks for this encoder
  941. * @encoder_type: user visible type of the encoder
  942. *
  943. * Initialises a preallocated encoder. Encoder should be
  944. * subclassed as part of driver encoder objects.
  945. *
  946. * Returns:
  947. * Zero on success, error code on failure.
  948. */
  949. int drm_encoder_init(struct drm_device *dev,
  950. struct drm_encoder *encoder,
  951. const struct drm_encoder_funcs *funcs,
  952. int encoder_type)
  953. {
  954. int ret;
  955. drm_modeset_lock_all(dev);
  956. ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
  957. if (ret)
  958. goto out_unlock;
  959. encoder->dev = dev;
  960. encoder->encoder_type = encoder_type;
  961. encoder->funcs = funcs;
  962. encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
  963. drm_encoder_enum_list[encoder_type].name,
  964. encoder->base.id);
  965. if (!encoder->name) {
  966. ret = -ENOMEM;
  967. goto out_put;
  968. }
  969. list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
  970. dev->mode_config.num_encoder++;
  971. out_put:
  972. if (ret)
  973. drm_mode_object_put(dev, &encoder->base);
  974. out_unlock:
  975. drm_modeset_unlock_all(dev);
  976. return ret;
  977. }
  978. EXPORT_SYMBOL(drm_encoder_init);
  979. /**
  980. * drm_encoder_cleanup - cleans up an initialised encoder
  981. * @encoder: encoder to cleanup
  982. *
  983. * Cleans up the encoder but doesn't free the object.
  984. */
  985. void drm_encoder_cleanup(struct drm_encoder *encoder)
  986. {
  987. struct drm_device *dev = encoder->dev;
  988. drm_modeset_lock_all(dev);
  989. drm_mode_object_put(dev, &encoder->base);
  990. kfree(encoder->name);
  991. list_del(&encoder->head);
  992. dev->mode_config.num_encoder--;
  993. drm_modeset_unlock_all(dev);
  994. memset(encoder, 0, sizeof(*encoder));
  995. }
  996. EXPORT_SYMBOL(drm_encoder_cleanup);
  997. /**
  998. * drm_universal_plane_init - Initialize a new universal plane object
  999. * @dev: DRM device
  1000. * @plane: plane object to init
  1001. * @possible_crtcs: bitmask of possible CRTCs
  1002. * @funcs: callbacks for the new plane
  1003. * @formats: array of supported formats (%DRM_FORMAT_*)
  1004. * @format_count: number of elements in @formats
  1005. * @type: type of plane (overlay, primary, cursor)
  1006. *
  1007. * Initializes a plane object of type @type.
  1008. *
  1009. * Returns:
  1010. * Zero on success, error code on failure.
  1011. */
  1012. int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
  1013. unsigned long possible_crtcs,
  1014. const struct drm_plane_funcs *funcs,
  1015. const uint32_t *formats, uint32_t format_count,
  1016. enum drm_plane_type type)
  1017. {
  1018. struct drm_mode_config *config = &dev->mode_config;
  1019. int ret;
  1020. ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
  1021. if (ret)
  1022. return ret;
  1023. drm_modeset_lock_init(&plane->mutex);
  1024. plane->base.properties = &plane->properties;
  1025. plane->dev = dev;
  1026. plane->funcs = funcs;
  1027. plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
  1028. GFP_KERNEL);
  1029. if (!plane->format_types) {
  1030. DRM_DEBUG_KMS("out of memory when allocating plane\n");
  1031. drm_mode_object_put(dev, &plane->base);
  1032. return -ENOMEM;
  1033. }
  1034. memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
  1035. plane->format_count = format_count;
  1036. plane->possible_crtcs = possible_crtcs;
  1037. plane->type = type;
  1038. list_add_tail(&plane->head, &config->plane_list);
  1039. config->num_total_plane++;
  1040. if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  1041. config->num_overlay_plane++;
  1042. drm_object_attach_property(&plane->base,
  1043. config->plane_type_property,
  1044. plane->type);
  1045. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  1046. drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
  1047. drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
  1048. drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
  1049. drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
  1050. drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
  1051. drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
  1052. drm_object_attach_property(&plane->base, config->prop_src_x, 0);
  1053. drm_object_attach_property(&plane->base, config->prop_src_y, 0);
  1054. drm_object_attach_property(&plane->base, config->prop_src_w, 0);
  1055. drm_object_attach_property(&plane->base, config->prop_src_h, 0);
  1056. }
  1057. return 0;
  1058. }
  1059. EXPORT_SYMBOL(drm_universal_plane_init);
  1060. /**
  1061. * drm_plane_init - Initialize a legacy plane
  1062. * @dev: DRM device
  1063. * @plane: plane object to init
  1064. * @possible_crtcs: bitmask of possible CRTCs
  1065. * @funcs: callbacks for the new plane
  1066. * @formats: array of supported formats (%DRM_FORMAT_*)
  1067. * @format_count: number of elements in @formats
  1068. * @is_primary: plane type (primary vs overlay)
  1069. *
  1070. * Legacy API to initialize a DRM plane.
  1071. *
  1072. * New drivers should call drm_universal_plane_init() instead.
  1073. *
  1074. * Returns:
  1075. * Zero on success, error code on failure.
  1076. */
  1077. int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
  1078. unsigned long possible_crtcs,
  1079. const struct drm_plane_funcs *funcs,
  1080. const uint32_t *formats, uint32_t format_count,
  1081. bool is_primary)
  1082. {
  1083. enum drm_plane_type type;
  1084. type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
  1085. return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
  1086. formats, format_count, type);
  1087. }
  1088. EXPORT_SYMBOL(drm_plane_init);
  1089. /**
  1090. * drm_plane_cleanup - Clean up the core plane usage
  1091. * @plane: plane to cleanup
  1092. *
  1093. * This function cleans up @plane and removes it from the DRM mode setting
  1094. * core. Note that the function does *not* free the plane structure itself,
  1095. * this is the responsibility of the caller.
  1096. */
  1097. void drm_plane_cleanup(struct drm_plane *plane)
  1098. {
  1099. struct drm_device *dev = plane->dev;
  1100. drm_modeset_lock_all(dev);
  1101. kfree(plane->format_types);
  1102. drm_mode_object_put(dev, &plane->base);
  1103. BUG_ON(list_empty(&plane->head));
  1104. list_del(&plane->head);
  1105. dev->mode_config.num_total_plane--;
  1106. if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  1107. dev->mode_config.num_overlay_plane--;
  1108. drm_modeset_unlock_all(dev);
  1109. WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
  1110. if (plane->state && plane->funcs->atomic_destroy_state)
  1111. plane->funcs->atomic_destroy_state(plane, plane->state);
  1112. memset(plane, 0, sizeof(*plane));
  1113. }
  1114. EXPORT_SYMBOL(drm_plane_cleanup);
  1115. /**
  1116. * drm_plane_index - find the index of a registered plane
  1117. * @plane: plane to find index for
  1118. *
  1119. * Given a registered plane, return the index of that CRTC within a DRM
  1120. * device's list of planes.
  1121. */
  1122. unsigned int drm_plane_index(struct drm_plane *plane)
  1123. {
  1124. unsigned int index = 0;
  1125. struct drm_plane *tmp;
  1126. list_for_each_entry(tmp, &plane->dev->mode_config.plane_list, head) {
  1127. if (tmp == plane)
  1128. return index;
  1129. index++;
  1130. }
  1131. BUG();
  1132. }
  1133. EXPORT_SYMBOL(drm_plane_index);
  1134. /**
  1135. * drm_plane_force_disable - Forcibly disable a plane
  1136. * @plane: plane to disable
  1137. *
  1138. * Forces the plane to be disabled.
  1139. *
  1140. * Used when the plane's current framebuffer is destroyed,
  1141. * and when restoring fbdev mode.
  1142. */
  1143. void drm_plane_force_disable(struct drm_plane *plane)
  1144. {
  1145. int ret;
  1146. if (!plane->fb)
  1147. return;
  1148. plane->old_fb = plane->fb;
  1149. ret = plane->funcs->disable_plane(plane);
  1150. if (ret) {
  1151. DRM_ERROR("failed to disable plane with busy fb\n");
  1152. plane->old_fb = NULL;
  1153. return;
  1154. }
  1155. /* disconnect the plane from the fb and crtc: */
  1156. __drm_framebuffer_unreference(plane->old_fb);
  1157. plane->old_fb = NULL;
  1158. plane->fb = NULL;
  1159. plane->crtc = NULL;
  1160. }
  1161. EXPORT_SYMBOL(drm_plane_force_disable);
  1162. static int drm_mode_create_standard_properties(struct drm_device *dev)
  1163. {
  1164. struct drm_property *prop;
  1165. /*
  1166. * Standard properties (apply to all connectors)
  1167. */
  1168. prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
  1169. DRM_MODE_PROP_IMMUTABLE,
  1170. "EDID", 0);
  1171. if (!prop)
  1172. return -ENOMEM;
  1173. dev->mode_config.edid_property = prop;
  1174. prop = drm_property_create_enum(dev, 0,
  1175. "DPMS", drm_dpms_enum_list,
  1176. ARRAY_SIZE(drm_dpms_enum_list));
  1177. if (!prop)
  1178. return -ENOMEM;
  1179. dev->mode_config.dpms_property = prop;
  1180. prop = drm_property_create(dev,
  1181. DRM_MODE_PROP_BLOB |
  1182. DRM_MODE_PROP_IMMUTABLE,
  1183. "PATH", 0);
  1184. if (!prop)
  1185. return -ENOMEM;
  1186. dev->mode_config.path_property = prop;
  1187. prop = drm_property_create(dev,
  1188. DRM_MODE_PROP_BLOB |
  1189. DRM_MODE_PROP_IMMUTABLE,
  1190. "TILE", 0);
  1191. if (!prop)
  1192. return -ENOMEM;
  1193. dev->mode_config.tile_property = prop;
  1194. prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1195. "type", drm_plane_type_enum_list,
  1196. ARRAY_SIZE(drm_plane_type_enum_list));
  1197. if (!prop)
  1198. return -ENOMEM;
  1199. dev->mode_config.plane_type_property = prop;
  1200. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1201. "SRC_X", 0, UINT_MAX);
  1202. if (!prop)
  1203. return -ENOMEM;
  1204. dev->mode_config.prop_src_x = prop;
  1205. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1206. "SRC_Y", 0, UINT_MAX);
  1207. if (!prop)
  1208. return -ENOMEM;
  1209. dev->mode_config.prop_src_y = prop;
  1210. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1211. "SRC_W", 0, UINT_MAX);
  1212. if (!prop)
  1213. return -ENOMEM;
  1214. dev->mode_config.prop_src_w = prop;
  1215. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1216. "SRC_H", 0, UINT_MAX);
  1217. if (!prop)
  1218. return -ENOMEM;
  1219. dev->mode_config.prop_src_h = prop;
  1220. prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
  1221. "CRTC_X", INT_MIN, INT_MAX);
  1222. if (!prop)
  1223. return -ENOMEM;
  1224. dev->mode_config.prop_crtc_x = prop;
  1225. prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
  1226. "CRTC_Y", INT_MIN, INT_MAX);
  1227. if (!prop)
  1228. return -ENOMEM;
  1229. dev->mode_config.prop_crtc_y = prop;
  1230. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1231. "CRTC_W", 0, INT_MAX);
  1232. if (!prop)
  1233. return -ENOMEM;
  1234. dev->mode_config.prop_crtc_w = prop;
  1235. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1236. "CRTC_H", 0, INT_MAX);
  1237. if (!prop)
  1238. return -ENOMEM;
  1239. dev->mode_config.prop_crtc_h = prop;
  1240. prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
  1241. "FB_ID", DRM_MODE_OBJECT_FB);
  1242. if (!prop)
  1243. return -ENOMEM;
  1244. dev->mode_config.prop_fb_id = prop;
  1245. prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
  1246. "CRTC_ID", DRM_MODE_OBJECT_CRTC);
  1247. if (!prop)
  1248. return -ENOMEM;
  1249. dev->mode_config.prop_crtc_id = prop;
  1250. return 0;
  1251. }
  1252. /**
  1253. * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
  1254. * @dev: DRM device
  1255. *
  1256. * Called by a driver the first time a DVI-I connector is made.
  1257. */
  1258. int drm_mode_create_dvi_i_properties(struct drm_device *dev)
  1259. {
  1260. struct drm_property *dvi_i_selector;
  1261. struct drm_property *dvi_i_subconnector;
  1262. if (dev->mode_config.dvi_i_select_subconnector_property)
  1263. return 0;
  1264. dvi_i_selector =
  1265. drm_property_create_enum(dev, 0,
  1266. "select subconnector",
  1267. drm_dvi_i_select_enum_list,
  1268. ARRAY_SIZE(drm_dvi_i_select_enum_list));
  1269. dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
  1270. dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1271. "subconnector",
  1272. drm_dvi_i_subconnector_enum_list,
  1273. ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
  1274. dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
  1275. return 0;
  1276. }
  1277. EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
  1278. /**
  1279. * drm_create_tv_properties - create TV specific connector properties
  1280. * @dev: DRM device
  1281. * @num_modes: number of different TV formats (modes) supported
  1282. * @modes: array of pointers to strings containing name of each format
  1283. *
  1284. * Called by a driver's TV initialization routine, this function creates
  1285. * the TV specific connector properties for a given device. Caller is
  1286. * responsible for allocating a list of format names and passing them to
  1287. * this routine.
  1288. */
  1289. int drm_mode_create_tv_properties(struct drm_device *dev,
  1290. unsigned int num_modes,
  1291. char *modes[])
  1292. {
  1293. struct drm_property *tv_selector;
  1294. struct drm_property *tv_subconnector;
  1295. unsigned int i;
  1296. if (dev->mode_config.tv_select_subconnector_property)
  1297. return 0;
  1298. /*
  1299. * Basic connector properties
  1300. */
  1301. tv_selector = drm_property_create_enum(dev, 0,
  1302. "select subconnector",
  1303. drm_tv_select_enum_list,
  1304. ARRAY_SIZE(drm_tv_select_enum_list));
  1305. dev->mode_config.tv_select_subconnector_property = tv_selector;
  1306. tv_subconnector =
  1307. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1308. "subconnector",
  1309. drm_tv_subconnector_enum_list,
  1310. ARRAY_SIZE(drm_tv_subconnector_enum_list));
  1311. dev->mode_config.tv_subconnector_property = tv_subconnector;
  1312. /*
  1313. * Other, TV specific properties: margins & TV modes.
  1314. */
  1315. dev->mode_config.tv_left_margin_property =
  1316. drm_property_create_range(dev, 0, "left margin", 0, 100);
  1317. dev->mode_config.tv_right_margin_property =
  1318. drm_property_create_range(dev, 0, "right margin", 0, 100);
  1319. dev->mode_config.tv_top_margin_property =
  1320. drm_property_create_range(dev, 0, "top margin", 0, 100);
  1321. dev->mode_config.tv_bottom_margin_property =
  1322. drm_property_create_range(dev, 0, "bottom margin", 0, 100);
  1323. dev->mode_config.tv_mode_property =
  1324. drm_property_create(dev, DRM_MODE_PROP_ENUM,
  1325. "mode", num_modes);
  1326. for (i = 0; i < num_modes; i++)
  1327. drm_property_add_enum(dev->mode_config.tv_mode_property, i,
  1328. i, modes[i]);
  1329. dev->mode_config.tv_brightness_property =
  1330. drm_property_create_range(dev, 0, "brightness", 0, 100);
  1331. dev->mode_config.tv_contrast_property =
  1332. drm_property_create_range(dev, 0, "contrast", 0, 100);
  1333. dev->mode_config.tv_flicker_reduction_property =
  1334. drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
  1335. dev->mode_config.tv_overscan_property =
  1336. drm_property_create_range(dev, 0, "overscan", 0, 100);
  1337. dev->mode_config.tv_saturation_property =
  1338. drm_property_create_range(dev, 0, "saturation", 0, 100);
  1339. dev->mode_config.tv_hue_property =
  1340. drm_property_create_range(dev, 0, "hue", 0, 100);
  1341. return 0;
  1342. }
  1343. EXPORT_SYMBOL(drm_mode_create_tv_properties);
  1344. /**
  1345. * drm_mode_create_scaling_mode_property - create scaling mode property
  1346. * @dev: DRM device
  1347. *
  1348. * Called by a driver the first time it's needed, must be attached to desired
  1349. * connectors.
  1350. */
  1351. int drm_mode_create_scaling_mode_property(struct drm_device *dev)
  1352. {
  1353. struct drm_property *scaling_mode;
  1354. if (dev->mode_config.scaling_mode_property)
  1355. return 0;
  1356. scaling_mode =
  1357. drm_property_create_enum(dev, 0, "scaling mode",
  1358. drm_scaling_mode_enum_list,
  1359. ARRAY_SIZE(drm_scaling_mode_enum_list));
  1360. dev->mode_config.scaling_mode_property = scaling_mode;
  1361. return 0;
  1362. }
  1363. EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
  1364. /**
  1365. * drm_mode_create_aspect_ratio_property - create aspect ratio property
  1366. * @dev: DRM device
  1367. *
  1368. * Called by a driver the first time it's needed, must be attached to desired
  1369. * connectors.
  1370. *
  1371. * Returns:
  1372. * Zero on success, negative errno on failure.
  1373. */
  1374. int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
  1375. {
  1376. if (dev->mode_config.aspect_ratio_property)
  1377. return 0;
  1378. dev->mode_config.aspect_ratio_property =
  1379. drm_property_create_enum(dev, 0, "aspect ratio",
  1380. drm_aspect_ratio_enum_list,
  1381. ARRAY_SIZE(drm_aspect_ratio_enum_list));
  1382. if (dev->mode_config.aspect_ratio_property == NULL)
  1383. return -ENOMEM;
  1384. return 0;
  1385. }
  1386. EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
  1387. /**
  1388. * drm_mode_create_dirty_property - create dirty property
  1389. * @dev: DRM device
  1390. *
  1391. * Called by a driver the first time it's needed, must be attached to desired
  1392. * connectors.
  1393. */
  1394. int drm_mode_create_dirty_info_property(struct drm_device *dev)
  1395. {
  1396. struct drm_property *dirty_info;
  1397. if (dev->mode_config.dirty_info_property)
  1398. return 0;
  1399. dirty_info =
  1400. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1401. "dirty",
  1402. drm_dirty_info_enum_list,
  1403. ARRAY_SIZE(drm_dirty_info_enum_list));
  1404. dev->mode_config.dirty_info_property = dirty_info;
  1405. return 0;
  1406. }
  1407. EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
  1408. /**
  1409. * drm_mode_create_suggested_offset_properties - create suggests offset properties
  1410. * @dev: DRM device
  1411. *
  1412. * Create the the suggested x/y offset property for connectors.
  1413. */
  1414. int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
  1415. {
  1416. if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
  1417. return 0;
  1418. dev->mode_config.suggested_x_property =
  1419. drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
  1420. dev->mode_config.suggested_y_property =
  1421. drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
  1422. if (dev->mode_config.suggested_x_property == NULL ||
  1423. dev->mode_config.suggested_y_property == NULL)
  1424. return -ENOMEM;
  1425. return 0;
  1426. }
  1427. EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
  1428. static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
  1429. {
  1430. uint32_t total_objects = 0;
  1431. total_objects += dev->mode_config.num_crtc;
  1432. total_objects += dev->mode_config.num_connector;
  1433. total_objects += dev->mode_config.num_encoder;
  1434. total_objects += dev->mode_config.num_bridge;
  1435. group->id_list = kcalloc(total_objects, sizeof(uint32_t), GFP_KERNEL);
  1436. if (!group->id_list)
  1437. return -ENOMEM;
  1438. group->num_crtcs = 0;
  1439. group->num_connectors = 0;
  1440. group->num_encoders = 0;
  1441. group->num_bridges = 0;
  1442. return 0;
  1443. }
  1444. void drm_mode_group_destroy(struct drm_mode_group *group)
  1445. {
  1446. kfree(group->id_list);
  1447. group->id_list = NULL;
  1448. }
  1449. /*
  1450. * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
  1451. * the drm core's responsibility to set up mode control groups.
  1452. */
  1453. int drm_mode_group_init_legacy_group(struct drm_device *dev,
  1454. struct drm_mode_group *group)
  1455. {
  1456. struct drm_crtc *crtc;
  1457. struct drm_encoder *encoder;
  1458. struct drm_connector *connector;
  1459. struct drm_bridge *bridge;
  1460. int ret;
  1461. ret = drm_mode_group_init(dev, group);
  1462. if (ret)
  1463. return ret;
  1464. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  1465. group->id_list[group->num_crtcs++] = crtc->base.id;
  1466. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  1467. group->id_list[group->num_crtcs + group->num_encoders++] =
  1468. encoder->base.id;
  1469. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  1470. group->id_list[group->num_crtcs + group->num_encoders +
  1471. group->num_connectors++] = connector->base.id;
  1472. list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
  1473. group->id_list[group->num_crtcs + group->num_encoders +
  1474. group->num_connectors + group->num_bridges++] =
  1475. bridge->base.id;
  1476. return 0;
  1477. }
  1478. EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
  1479. void drm_reinit_primary_mode_group(struct drm_device *dev)
  1480. {
  1481. drm_modeset_lock_all(dev);
  1482. drm_mode_group_destroy(&dev->primary->mode_group);
  1483. drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
  1484. drm_modeset_unlock_all(dev);
  1485. }
  1486. EXPORT_SYMBOL(drm_reinit_primary_mode_group);
  1487. /**
  1488. * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
  1489. * @out: drm_mode_modeinfo struct to return to the user
  1490. * @in: drm_display_mode to use
  1491. *
  1492. * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
  1493. * the user.
  1494. */
  1495. static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
  1496. const struct drm_display_mode *in)
  1497. {
  1498. WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
  1499. in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
  1500. in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
  1501. in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
  1502. in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
  1503. "timing values too large for mode info\n");
  1504. out->clock = in->clock;
  1505. out->hdisplay = in->hdisplay;
  1506. out->hsync_start = in->hsync_start;
  1507. out->hsync_end = in->hsync_end;
  1508. out->htotal = in->htotal;
  1509. out->hskew = in->hskew;
  1510. out->vdisplay = in->vdisplay;
  1511. out->vsync_start = in->vsync_start;
  1512. out->vsync_end = in->vsync_end;
  1513. out->vtotal = in->vtotal;
  1514. out->vscan = in->vscan;
  1515. out->vrefresh = in->vrefresh;
  1516. out->flags = in->flags;
  1517. out->type = in->type;
  1518. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  1519. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  1520. }
  1521. /**
  1522. * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
  1523. * @out: drm_display_mode to return to the user
  1524. * @in: drm_mode_modeinfo to use
  1525. *
  1526. * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
  1527. * the caller.
  1528. *
  1529. * Returns:
  1530. * Zero on success, negative errno on failure.
  1531. */
  1532. static int drm_crtc_convert_umode(struct drm_display_mode *out,
  1533. const struct drm_mode_modeinfo *in)
  1534. {
  1535. if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
  1536. return -ERANGE;
  1537. if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
  1538. return -EINVAL;
  1539. out->clock = in->clock;
  1540. out->hdisplay = in->hdisplay;
  1541. out->hsync_start = in->hsync_start;
  1542. out->hsync_end = in->hsync_end;
  1543. out->htotal = in->htotal;
  1544. out->hskew = in->hskew;
  1545. out->vdisplay = in->vdisplay;
  1546. out->vsync_start = in->vsync_start;
  1547. out->vsync_end = in->vsync_end;
  1548. out->vtotal = in->vtotal;
  1549. out->vscan = in->vscan;
  1550. out->vrefresh = in->vrefresh;
  1551. out->flags = in->flags;
  1552. out->type = in->type;
  1553. strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  1554. out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  1555. return 0;
  1556. }
  1557. /**
  1558. * drm_mode_getresources - get graphics configuration
  1559. * @dev: drm device for the ioctl
  1560. * @data: data pointer for the ioctl
  1561. * @file_priv: drm file for the ioctl call
  1562. *
  1563. * Construct a set of configuration description structures and return
  1564. * them to the user, including CRTC, connector and framebuffer configuration.
  1565. *
  1566. * Called by the user via ioctl.
  1567. *
  1568. * Returns:
  1569. * Zero on success, negative errno on failure.
  1570. */
  1571. int drm_mode_getresources(struct drm_device *dev, void *data,
  1572. struct drm_file *file_priv)
  1573. {
  1574. struct drm_mode_card_res *card_res = data;
  1575. struct list_head *lh;
  1576. struct drm_framebuffer *fb;
  1577. struct drm_connector *connector;
  1578. struct drm_crtc *crtc;
  1579. struct drm_encoder *encoder;
  1580. int ret = 0;
  1581. int connector_count = 0;
  1582. int crtc_count = 0;
  1583. int fb_count = 0;
  1584. int encoder_count = 0;
  1585. int copied = 0, i;
  1586. uint32_t __user *fb_id;
  1587. uint32_t __user *crtc_id;
  1588. uint32_t __user *connector_id;
  1589. uint32_t __user *encoder_id;
  1590. struct drm_mode_group *mode_group;
  1591. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1592. return -EINVAL;
  1593. mutex_lock(&file_priv->fbs_lock);
  1594. /*
  1595. * For the non-control nodes we need to limit the list of resources
  1596. * by IDs in the group list for this node
  1597. */
  1598. list_for_each(lh, &file_priv->fbs)
  1599. fb_count++;
  1600. /* handle this in 4 parts */
  1601. /* FBs */
  1602. if (card_res->count_fbs >= fb_count) {
  1603. copied = 0;
  1604. fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
  1605. list_for_each_entry(fb, &file_priv->fbs, filp_head) {
  1606. if (put_user(fb->base.id, fb_id + copied)) {
  1607. mutex_unlock(&file_priv->fbs_lock);
  1608. return -EFAULT;
  1609. }
  1610. copied++;
  1611. }
  1612. }
  1613. card_res->count_fbs = fb_count;
  1614. mutex_unlock(&file_priv->fbs_lock);
  1615. /* mode_config.mutex protects the connector list against e.g. DP MST
  1616. * connector hot-adding. CRTC/Plane lists are invariant. */
  1617. mutex_lock(&dev->mode_config.mutex);
  1618. if (!drm_is_primary_client(file_priv)) {
  1619. mode_group = NULL;
  1620. list_for_each(lh, &dev->mode_config.crtc_list)
  1621. crtc_count++;
  1622. list_for_each(lh, &dev->mode_config.connector_list)
  1623. connector_count++;
  1624. list_for_each(lh, &dev->mode_config.encoder_list)
  1625. encoder_count++;
  1626. } else {
  1627. mode_group = &file_priv->master->minor->mode_group;
  1628. crtc_count = mode_group->num_crtcs;
  1629. connector_count = mode_group->num_connectors;
  1630. encoder_count = mode_group->num_encoders;
  1631. }
  1632. card_res->max_height = dev->mode_config.max_height;
  1633. card_res->min_height = dev->mode_config.min_height;
  1634. card_res->max_width = dev->mode_config.max_width;
  1635. card_res->min_width = dev->mode_config.min_width;
  1636. /* CRTCs */
  1637. if (card_res->count_crtcs >= crtc_count) {
  1638. copied = 0;
  1639. crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
  1640. if (!mode_group) {
  1641. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  1642. head) {
  1643. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  1644. if (put_user(crtc->base.id, crtc_id + copied)) {
  1645. ret = -EFAULT;
  1646. goto out;
  1647. }
  1648. copied++;
  1649. }
  1650. } else {
  1651. for (i = 0; i < mode_group->num_crtcs; i++) {
  1652. if (put_user(mode_group->id_list[i],
  1653. crtc_id + copied)) {
  1654. ret = -EFAULT;
  1655. goto out;
  1656. }
  1657. copied++;
  1658. }
  1659. }
  1660. }
  1661. card_res->count_crtcs = crtc_count;
  1662. /* Encoders */
  1663. if (card_res->count_encoders >= encoder_count) {
  1664. copied = 0;
  1665. encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
  1666. if (!mode_group) {
  1667. list_for_each_entry(encoder,
  1668. &dev->mode_config.encoder_list,
  1669. head) {
  1670. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
  1671. encoder->name);
  1672. if (put_user(encoder->base.id, encoder_id +
  1673. copied)) {
  1674. ret = -EFAULT;
  1675. goto out;
  1676. }
  1677. copied++;
  1678. }
  1679. } else {
  1680. for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
  1681. if (put_user(mode_group->id_list[i],
  1682. encoder_id + copied)) {
  1683. ret = -EFAULT;
  1684. goto out;
  1685. }
  1686. copied++;
  1687. }
  1688. }
  1689. }
  1690. card_res->count_encoders = encoder_count;
  1691. /* Connectors */
  1692. if (card_res->count_connectors >= connector_count) {
  1693. copied = 0;
  1694. connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
  1695. if (!mode_group) {
  1696. list_for_each_entry(connector,
  1697. &dev->mode_config.connector_list,
  1698. head) {
  1699. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1700. connector->base.id,
  1701. connector->name);
  1702. if (put_user(connector->base.id,
  1703. connector_id + copied)) {
  1704. ret = -EFAULT;
  1705. goto out;
  1706. }
  1707. copied++;
  1708. }
  1709. } else {
  1710. int start = mode_group->num_crtcs +
  1711. mode_group->num_encoders;
  1712. for (i = start; i < start + mode_group->num_connectors; i++) {
  1713. if (put_user(mode_group->id_list[i],
  1714. connector_id + copied)) {
  1715. ret = -EFAULT;
  1716. goto out;
  1717. }
  1718. copied++;
  1719. }
  1720. }
  1721. }
  1722. card_res->count_connectors = connector_count;
  1723. DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
  1724. card_res->count_connectors, card_res->count_encoders);
  1725. out:
  1726. mutex_unlock(&dev->mode_config.mutex);
  1727. return ret;
  1728. }
  1729. /**
  1730. * drm_mode_getcrtc - get CRTC configuration
  1731. * @dev: drm device for the ioctl
  1732. * @data: data pointer for the ioctl
  1733. * @file_priv: drm file for the ioctl call
  1734. *
  1735. * Construct a CRTC configuration structure to return to the user.
  1736. *
  1737. * Called by the user via ioctl.
  1738. *
  1739. * Returns:
  1740. * Zero on success, negative errno on failure.
  1741. */
  1742. int drm_mode_getcrtc(struct drm_device *dev,
  1743. void *data, struct drm_file *file_priv)
  1744. {
  1745. struct drm_mode_crtc *crtc_resp = data;
  1746. struct drm_crtc *crtc;
  1747. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1748. return -EINVAL;
  1749. crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
  1750. if (!crtc)
  1751. return -ENOENT;
  1752. drm_modeset_lock_crtc(crtc, crtc->primary);
  1753. crtc_resp->x = crtc->x;
  1754. crtc_resp->y = crtc->y;
  1755. crtc_resp->gamma_size = crtc->gamma_size;
  1756. if (crtc->primary->fb)
  1757. crtc_resp->fb_id = crtc->primary->fb->base.id;
  1758. else
  1759. crtc_resp->fb_id = 0;
  1760. if (crtc->enabled) {
  1761. drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
  1762. crtc_resp->mode_valid = 1;
  1763. } else {
  1764. crtc_resp->mode_valid = 0;
  1765. }
  1766. drm_modeset_unlock_crtc(crtc);
  1767. return 0;
  1768. }
  1769. static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
  1770. const struct drm_file *file_priv)
  1771. {
  1772. /*
  1773. * If user-space hasn't configured the driver to expose the stereo 3D
  1774. * modes, don't expose them.
  1775. */
  1776. if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
  1777. return false;
  1778. return true;
  1779. }
  1780. static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
  1781. {
  1782. /* For atomic drivers only state objects are synchronously updated and
  1783. * protected by modeset locks, so check those first. */
  1784. if (connector->state)
  1785. return connector->state->best_encoder;
  1786. return connector->encoder;
  1787. }
  1788. /* helper for getconnector and getproperties ioctls */
  1789. static int get_properties(struct drm_mode_object *obj, bool atomic,
  1790. uint32_t __user *prop_ptr, uint64_t __user *prop_values,
  1791. uint32_t *arg_count_props)
  1792. {
  1793. int props_count;
  1794. int i, ret, copied;
  1795. props_count = obj->properties->count;
  1796. if (!atomic)
  1797. props_count -= obj->properties->atomic_count;
  1798. if ((*arg_count_props >= props_count) && props_count) {
  1799. for (i = 0, copied = 0; copied < props_count; i++) {
  1800. struct drm_property *prop = obj->properties->properties[i];
  1801. uint64_t val;
  1802. if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
  1803. continue;
  1804. ret = drm_object_property_get_value(obj, prop, &val);
  1805. if (ret)
  1806. return ret;
  1807. if (put_user(prop->base.id, prop_ptr + copied))
  1808. return -EFAULT;
  1809. if (put_user(val, prop_values + copied))
  1810. return -EFAULT;
  1811. copied++;
  1812. }
  1813. }
  1814. *arg_count_props = props_count;
  1815. return 0;
  1816. }
  1817. /**
  1818. * drm_mode_getconnector - get connector configuration
  1819. * @dev: drm device for the ioctl
  1820. * @data: data pointer for the ioctl
  1821. * @file_priv: drm file for the ioctl call
  1822. *
  1823. * Construct a connector configuration structure to return to the user.
  1824. *
  1825. * Called by the user via ioctl.
  1826. *
  1827. * Returns:
  1828. * Zero on success, negative errno on failure.
  1829. */
  1830. int drm_mode_getconnector(struct drm_device *dev, void *data,
  1831. struct drm_file *file_priv)
  1832. {
  1833. struct drm_mode_get_connector *out_resp = data;
  1834. struct drm_connector *connector;
  1835. struct drm_encoder *encoder;
  1836. struct drm_display_mode *mode;
  1837. int mode_count = 0;
  1838. int encoders_count = 0;
  1839. int ret = 0;
  1840. int copied = 0;
  1841. int i;
  1842. struct drm_mode_modeinfo u_mode;
  1843. struct drm_mode_modeinfo __user *mode_ptr;
  1844. uint32_t __user *encoder_ptr;
  1845. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1846. return -EINVAL;
  1847. memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  1848. DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
  1849. mutex_lock(&dev->mode_config.mutex);
  1850. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  1851. connector = drm_connector_find(dev, out_resp->connector_id);
  1852. if (!connector) {
  1853. ret = -ENOENT;
  1854. goto out;
  1855. }
  1856. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
  1857. if (connector->encoder_ids[i] != 0)
  1858. encoders_count++;
  1859. if (out_resp->count_modes == 0) {
  1860. connector->funcs->fill_modes(connector,
  1861. dev->mode_config.max_width,
  1862. dev->mode_config.max_height);
  1863. }
  1864. /* delayed so we get modes regardless of pre-fill_modes state */
  1865. list_for_each_entry(mode, &connector->modes, head)
  1866. if (drm_mode_expose_to_userspace(mode, file_priv))
  1867. mode_count++;
  1868. out_resp->connector_id = connector->base.id;
  1869. out_resp->connector_type = connector->connector_type;
  1870. out_resp->connector_type_id = connector->connector_type_id;
  1871. out_resp->mm_width = connector->display_info.width_mm;
  1872. out_resp->mm_height = connector->display_info.height_mm;
  1873. out_resp->subpixel = connector->display_info.subpixel_order;
  1874. out_resp->connection = connector->status;
  1875. encoder = drm_connector_get_encoder(connector);
  1876. if (encoder)
  1877. out_resp->encoder_id = encoder->base.id;
  1878. else
  1879. out_resp->encoder_id = 0;
  1880. /*
  1881. * This ioctl is called twice, once to determine how much space is
  1882. * needed, and the 2nd time to fill it.
  1883. */
  1884. if ((out_resp->count_modes >= mode_count) && mode_count) {
  1885. copied = 0;
  1886. mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
  1887. list_for_each_entry(mode, &connector->modes, head) {
  1888. if (!drm_mode_expose_to_userspace(mode, file_priv))
  1889. continue;
  1890. drm_crtc_convert_to_umode(&u_mode, mode);
  1891. if (copy_to_user(mode_ptr + copied,
  1892. &u_mode, sizeof(u_mode))) {
  1893. ret = -EFAULT;
  1894. goto out;
  1895. }
  1896. copied++;
  1897. }
  1898. }
  1899. out_resp->count_modes = mode_count;
  1900. ret = get_properties(&connector->base, file_priv->atomic,
  1901. (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
  1902. (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
  1903. &out_resp->count_props);
  1904. if (ret)
  1905. goto out;
  1906. if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  1907. copied = 0;
  1908. encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
  1909. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1910. if (connector->encoder_ids[i] != 0) {
  1911. if (put_user(connector->encoder_ids[i],
  1912. encoder_ptr + copied)) {
  1913. ret = -EFAULT;
  1914. goto out;
  1915. }
  1916. copied++;
  1917. }
  1918. }
  1919. }
  1920. out_resp->count_encoders = encoders_count;
  1921. out:
  1922. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  1923. mutex_unlock(&dev->mode_config.mutex);
  1924. return ret;
  1925. }
  1926. static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
  1927. {
  1928. struct drm_connector *connector;
  1929. struct drm_device *dev = encoder->dev;
  1930. bool uses_atomic = false;
  1931. /* For atomic drivers only state objects are synchronously updated and
  1932. * protected by modeset locks, so check those first. */
  1933. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1934. if (!connector->state)
  1935. continue;
  1936. uses_atomic = true;
  1937. if (connector->state->best_encoder != encoder)
  1938. continue;
  1939. return connector->state->crtc;
  1940. }
  1941. /* Don't return stale data (e.g. pending async disable). */
  1942. if (uses_atomic)
  1943. return NULL;
  1944. return encoder->crtc;
  1945. }
  1946. /**
  1947. * drm_mode_getencoder - get encoder configuration
  1948. * @dev: drm device for the ioctl
  1949. * @data: data pointer for the ioctl
  1950. * @file_priv: drm file for the ioctl call
  1951. *
  1952. * Construct a encoder configuration structure to return to the user.
  1953. *
  1954. * Called by the user via ioctl.
  1955. *
  1956. * Returns:
  1957. * Zero on success, negative errno on failure.
  1958. */
  1959. int drm_mode_getencoder(struct drm_device *dev, void *data,
  1960. struct drm_file *file_priv)
  1961. {
  1962. struct drm_mode_get_encoder *enc_resp = data;
  1963. struct drm_encoder *encoder;
  1964. struct drm_crtc *crtc;
  1965. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1966. return -EINVAL;
  1967. encoder = drm_encoder_find(dev, enc_resp->encoder_id);
  1968. if (!encoder)
  1969. return -ENOENT;
  1970. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  1971. crtc = drm_encoder_get_crtc(encoder);
  1972. if (crtc)
  1973. enc_resp->crtc_id = crtc->base.id;
  1974. else if (encoder->crtc)
  1975. enc_resp->crtc_id = encoder->crtc->base.id;
  1976. else
  1977. enc_resp->crtc_id = 0;
  1978. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  1979. enc_resp->encoder_type = encoder->encoder_type;
  1980. enc_resp->encoder_id = encoder->base.id;
  1981. enc_resp->possible_crtcs = encoder->possible_crtcs;
  1982. enc_resp->possible_clones = encoder->possible_clones;
  1983. return 0;
  1984. }
  1985. /**
  1986. * drm_mode_getplane_res - enumerate all plane resources
  1987. * @dev: DRM device
  1988. * @data: ioctl data
  1989. * @file_priv: DRM file info
  1990. *
  1991. * Construct a list of plane ids to return to the user.
  1992. *
  1993. * Called by the user via ioctl.
  1994. *
  1995. * Returns:
  1996. * Zero on success, negative errno on failure.
  1997. */
  1998. int drm_mode_getplane_res(struct drm_device *dev, void *data,
  1999. struct drm_file *file_priv)
  2000. {
  2001. struct drm_mode_get_plane_res *plane_resp = data;
  2002. struct drm_mode_config *config;
  2003. struct drm_plane *plane;
  2004. uint32_t __user *plane_ptr;
  2005. int copied = 0;
  2006. unsigned num_planes;
  2007. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2008. return -EINVAL;
  2009. config = &dev->mode_config;
  2010. if (file_priv->universal_planes)
  2011. num_planes = config->num_total_plane;
  2012. else
  2013. num_planes = config->num_overlay_plane;
  2014. /*
  2015. * This ioctl is called twice, once to determine how much space is
  2016. * needed, and the 2nd time to fill it.
  2017. */
  2018. if (num_planes &&
  2019. (plane_resp->count_planes >= num_planes)) {
  2020. plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
  2021. /* Plane lists are invariant, no locking needed. */
  2022. list_for_each_entry(plane, &config->plane_list, head) {
  2023. /*
  2024. * Unless userspace set the 'universal planes'
  2025. * capability bit, only advertise overlays.
  2026. */
  2027. if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
  2028. !file_priv->universal_planes)
  2029. continue;
  2030. if (put_user(plane->base.id, plane_ptr + copied))
  2031. return -EFAULT;
  2032. copied++;
  2033. }
  2034. }
  2035. plane_resp->count_planes = num_planes;
  2036. return 0;
  2037. }
  2038. /**
  2039. * drm_mode_getplane - get plane configuration
  2040. * @dev: DRM device
  2041. * @data: ioctl data
  2042. * @file_priv: DRM file info
  2043. *
  2044. * Construct a plane configuration structure to return to the user.
  2045. *
  2046. * Called by the user via ioctl.
  2047. *
  2048. * Returns:
  2049. * Zero on success, negative errno on failure.
  2050. */
  2051. int drm_mode_getplane(struct drm_device *dev, void *data,
  2052. struct drm_file *file_priv)
  2053. {
  2054. struct drm_mode_get_plane *plane_resp = data;
  2055. struct drm_plane *plane;
  2056. uint32_t __user *format_ptr;
  2057. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2058. return -EINVAL;
  2059. plane = drm_plane_find(dev, plane_resp->plane_id);
  2060. if (!plane)
  2061. return -ENOENT;
  2062. drm_modeset_lock(&plane->mutex, NULL);
  2063. if (plane->crtc)
  2064. plane_resp->crtc_id = plane->crtc->base.id;
  2065. else
  2066. plane_resp->crtc_id = 0;
  2067. if (plane->fb)
  2068. plane_resp->fb_id = plane->fb->base.id;
  2069. else
  2070. plane_resp->fb_id = 0;
  2071. drm_modeset_unlock(&plane->mutex);
  2072. plane_resp->plane_id = plane->base.id;
  2073. plane_resp->possible_crtcs = plane->possible_crtcs;
  2074. plane_resp->gamma_size = 0;
  2075. /*
  2076. * This ioctl is called twice, once to determine how much space is
  2077. * needed, and the 2nd time to fill it.
  2078. */
  2079. if (plane->format_count &&
  2080. (plane_resp->count_format_types >= plane->format_count)) {
  2081. format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
  2082. if (copy_to_user(format_ptr,
  2083. plane->format_types,
  2084. sizeof(uint32_t) * plane->format_count)) {
  2085. return -EFAULT;
  2086. }
  2087. }
  2088. plane_resp->count_format_types = plane->format_count;
  2089. return 0;
  2090. }
  2091. /*
  2092. * setplane_internal - setplane handler for internal callers
  2093. *
  2094. * Note that we assume an extra reference has already been taken on fb. If the
  2095. * update fails, this reference will be dropped before return; if it succeeds,
  2096. * the previous framebuffer (if any) will be unreferenced instead.
  2097. *
  2098. * src_{x,y,w,h} are provided in 16.16 fixed point format
  2099. */
  2100. static int __setplane_internal(struct drm_plane *plane,
  2101. struct drm_crtc *crtc,
  2102. struct drm_framebuffer *fb,
  2103. int32_t crtc_x, int32_t crtc_y,
  2104. uint32_t crtc_w, uint32_t crtc_h,
  2105. /* src_{x,y,w,h} values are 16.16 fixed point */
  2106. uint32_t src_x, uint32_t src_y,
  2107. uint32_t src_w, uint32_t src_h)
  2108. {
  2109. int ret = 0;
  2110. unsigned int fb_width, fb_height;
  2111. unsigned int i;
  2112. /* No fb means shut it down */
  2113. if (!fb) {
  2114. plane->old_fb = plane->fb;
  2115. ret = plane->funcs->disable_plane(plane);
  2116. if (!ret) {
  2117. plane->crtc = NULL;
  2118. plane->fb = NULL;
  2119. } else {
  2120. plane->old_fb = NULL;
  2121. }
  2122. goto out;
  2123. }
  2124. /* Check whether this plane is usable on this CRTC */
  2125. if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
  2126. DRM_DEBUG_KMS("Invalid crtc for plane\n");
  2127. ret = -EINVAL;
  2128. goto out;
  2129. }
  2130. /* Check whether this plane supports the fb pixel format. */
  2131. for (i = 0; i < plane->format_count; i++)
  2132. if (fb->pixel_format == plane->format_types[i])
  2133. break;
  2134. if (i == plane->format_count) {
  2135. DRM_DEBUG_KMS("Invalid pixel format %s\n",
  2136. drm_get_format_name(fb->pixel_format));
  2137. ret = -EINVAL;
  2138. goto out;
  2139. }
  2140. fb_width = fb->width << 16;
  2141. fb_height = fb->height << 16;
  2142. /* Make sure source coordinates are inside the fb. */
  2143. if (src_w > fb_width ||
  2144. src_x > fb_width - src_w ||
  2145. src_h > fb_height ||
  2146. src_y > fb_height - src_h) {
  2147. DRM_DEBUG_KMS("Invalid source coordinates "
  2148. "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
  2149. src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
  2150. src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
  2151. src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
  2152. src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
  2153. ret = -ENOSPC;
  2154. goto out;
  2155. }
  2156. plane->old_fb = plane->fb;
  2157. ret = plane->funcs->update_plane(plane, crtc, fb,
  2158. crtc_x, crtc_y, crtc_w, crtc_h,
  2159. src_x, src_y, src_w, src_h);
  2160. if (!ret) {
  2161. plane->crtc = crtc;
  2162. plane->fb = fb;
  2163. fb = NULL;
  2164. } else {
  2165. plane->old_fb = NULL;
  2166. }
  2167. out:
  2168. if (fb)
  2169. drm_framebuffer_unreference(fb);
  2170. if (plane->old_fb)
  2171. drm_framebuffer_unreference(plane->old_fb);
  2172. plane->old_fb = NULL;
  2173. return ret;
  2174. }
  2175. static int setplane_internal(struct drm_plane *plane,
  2176. struct drm_crtc *crtc,
  2177. struct drm_framebuffer *fb,
  2178. int32_t crtc_x, int32_t crtc_y,
  2179. uint32_t crtc_w, uint32_t crtc_h,
  2180. /* src_{x,y,w,h} values are 16.16 fixed point */
  2181. uint32_t src_x, uint32_t src_y,
  2182. uint32_t src_w, uint32_t src_h)
  2183. {
  2184. int ret;
  2185. drm_modeset_lock_all(plane->dev);
  2186. ret = __setplane_internal(plane, crtc, fb,
  2187. crtc_x, crtc_y, crtc_w, crtc_h,
  2188. src_x, src_y, src_w, src_h);
  2189. drm_modeset_unlock_all(plane->dev);
  2190. return ret;
  2191. }
  2192. /**
  2193. * drm_mode_setplane - configure a plane's configuration
  2194. * @dev: DRM device
  2195. * @data: ioctl data*
  2196. * @file_priv: DRM file info
  2197. *
  2198. * Set plane configuration, including placement, fb, scaling, and other factors.
  2199. * Or pass a NULL fb to disable (planes may be disabled without providing a
  2200. * valid crtc).
  2201. *
  2202. * Returns:
  2203. * Zero on success, negative errno on failure.
  2204. */
  2205. int drm_mode_setplane(struct drm_device *dev, void *data,
  2206. struct drm_file *file_priv)
  2207. {
  2208. struct drm_mode_set_plane *plane_req = data;
  2209. struct drm_plane *plane;
  2210. struct drm_crtc *crtc = NULL;
  2211. struct drm_framebuffer *fb = NULL;
  2212. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2213. return -EINVAL;
  2214. /* Give drivers some help against integer overflows */
  2215. if (plane_req->crtc_w > INT_MAX ||
  2216. plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
  2217. plane_req->crtc_h > INT_MAX ||
  2218. plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
  2219. DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
  2220. plane_req->crtc_w, plane_req->crtc_h,
  2221. plane_req->crtc_x, plane_req->crtc_y);
  2222. return -ERANGE;
  2223. }
  2224. /*
  2225. * First, find the plane, crtc, and fb objects. If not available,
  2226. * we don't bother to call the driver.
  2227. */
  2228. plane = drm_plane_find(dev, plane_req->plane_id);
  2229. if (!plane) {
  2230. DRM_DEBUG_KMS("Unknown plane ID %d\n",
  2231. plane_req->plane_id);
  2232. return -ENOENT;
  2233. }
  2234. if (plane_req->fb_id) {
  2235. fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
  2236. if (!fb) {
  2237. DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
  2238. plane_req->fb_id);
  2239. return -ENOENT;
  2240. }
  2241. crtc = drm_crtc_find(dev, plane_req->crtc_id);
  2242. if (!crtc) {
  2243. DRM_DEBUG_KMS("Unknown crtc ID %d\n",
  2244. plane_req->crtc_id);
  2245. return -ENOENT;
  2246. }
  2247. }
  2248. /*
  2249. * setplane_internal will take care of deref'ing either the old or new
  2250. * framebuffer depending on success.
  2251. */
  2252. return setplane_internal(plane, crtc, fb,
  2253. plane_req->crtc_x, plane_req->crtc_y,
  2254. plane_req->crtc_w, plane_req->crtc_h,
  2255. plane_req->src_x, plane_req->src_y,
  2256. plane_req->src_w, plane_req->src_h);
  2257. }
  2258. /**
  2259. * drm_mode_set_config_internal - helper to call ->set_config
  2260. * @set: modeset config to set
  2261. *
  2262. * This is a little helper to wrap internal calls to the ->set_config driver
  2263. * interface. The only thing it adds is correct refcounting dance.
  2264. *
  2265. * Returns:
  2266. * Zero on success, negative errno on failure.
  2267. */
  2268. int drm_mode_set_config_internal(struct drm_mode_set *set)
  2269. {
  2270. struct drm_crtc *crtc = set->crtc;
  2271. struct drm_framebuffer *fb;
  2272. struct drm_crtc *tmp;
  2273. int ret;
  2274. /*
  2275. * NOTE: ->set_config can also disable other crtcs (if we steal all
  2276. * connectors from it), hence we need to refcount the fbs across all
  2277. * crtcs. Atomic modeset will have saner semantics ...
  2278. */
  2279. list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
  2280. tmp->primary->old_fb = tmp->primary->fb;
  2281. fb = set->fb;
  2282. ret = crtc->funcs->set_config(set);
  2283. if (ret == 0) {
  2284. crtc->primary->crtc = crtc;
  2285. crtc->primary->fb = fb;
  2286. }
  2287. list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
  2288. if (tmp->primary->fb)
  2289. drm_framebuffer_reference(tmp->primary->fb);
  2290. if (tmp->primary->old_fb)
  2291. drm_framebuffer_unreference(tmp->primary->old_fb);
  2292. tmp->primary->old_fb = NULL;
  2293. }
  2294. return ret;
  2295. }
  2296. EXPORT_SYMBOL(drm_mode_set_config_internal);
  2297. /**
  2298. * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
  2299. * CRTC viewport
  2300. * @crtc: CRTC that framebuffer will be displayed on
  2301. * @x: x panning
  2302. * @y: y panning
  2303. * @mode: mode that framebuffer will be displayed under
  2304. * @fb: framebuffer to check size of
  2305. */
  2306. int drm_crtc_check_viewport(const struct drm_crtc *crtc,
  2307. int x, int y,
  2308. const struct drm_display_mode *mode,
  2309. const struct drm_framebuffer *fb)
  2310. {
  2311. int hdisplay, vdisplay;
  2312. hdisplay = mode->hdisplay;
  2313. vdisplay = mode->vdisplay;
  2314. if (drm_mode_is_stereo(mode)) {
  2315. struct drm_display_mode adjusted = *mode;
  2316. drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
  2317. hdisplay = adjusted.crtc_hdisplay;
  2318. vdisplay = adjusted.crtc_vdisplay;
  2319. }
  2320. if (crtc->invert_dimensions)
  2321. swap(hdisplay, vdisplay);
  2322. if (hdisplay > fb->width ||
  2323. vdisplay > fb->height ||
  2324. x > fb->width - hdisplay ||
  2325. y > fb->height - vdisplay) {
  2326. DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
  2327. fb->width, fb->height, hdisplay, vdisplay, x, y,
  2328. crtc->invert_dimensions ? " (inverted)" : "");
  2329. return -ENOSPC;
  2330. }
  2331. return 0;
  2332. }
  2333. EXPORT_SYMBOL(drm_crtc_check_viewport);
  2334. /**
  2335. * drm_mode_setcrtc - set CRTC configuration
  2336. * @dev: drm device for the ioctl
  2337. * @data: data pointer for the ioctl
  2338. * @file_priv: drm file for the ioctl call
  2339. *
  2340. * Build a new CRTC configuration based on user request.
  2341. *
  2342. * Called by the user via ioctl.
  2343. *
  2344. * Returns:
  2345. * Zero on success, negative errno on failure.
  2346. */
  2347. int drm_mode_setcrtc(struct drm_device *dev, void *data,
  2348. struct drm_file *file_priv)
  2349. {
  2350. struct drm_mode_config *config = &dev->mode_config;
  2351. struct drm_mode_crtc *crtc_req = data;
  2352. struct drm_crtc *crtc;
  2353. struct drm_connector **connector_set = NULL, *connector;
  2354. struct drm_framebuffer *fb = NULL;
  2355. struct drm_display_mode *mode = NULL;
  2356. struct drm_mode_set set;
  2357. uint32_t __user *set_connectors_ptr;
  2358. int ret;
  2359. int i;
  2360. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2361. return -EINVAL;
  2362. /* For some reason crtc x/y offsets are signed internally. */
  2363. if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
  2364. return -ERANGE;
  2365. drm_modeset_lock_all(dev);
  2366. crtc = drm_crtc_find(dev, crtc_req->crtc_id);
  2367. if (!crtc) {
  2368. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
  2369. ret = -ENOENT;
  2370. goto out;
  2371. }
  2372. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  2373. if (crtc_req->mode_valid) {
  2374. /* If we have a mode we need a framebuffer. */
  2375. /* If we pass -1, set the mode with the currently bound fb */
  2376. if (crtc_req->fb_id == -1) {
  2377. if (!crtc->primary->fb) {
  2378. DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
  2379. ret = -EINVAL;
  2380. goto out;
  2381. }
  2382. fb = crtc->primary->fb;
  2383. /* Make refcounting symmetric with the lookup path. */
  2384. drm_framebuffer_reference(fb);
  2385. } else {
  2386. fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
  2387. if (!fb) {
  2388. DRM_DEBUG_KMS("Unknown FB ID%d\n",
  2389. crtc_req->fb_id);
  2390. ret = -ENOENT;
  2391. goto out;
  2392. }
  2393. }
  2394. mode = drm_mode_create(dev);
  2395. if (!mode) {
  2396. ret = -ENOMEM;
  2397. goto out;
  2398. }
  2399. ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
  2400. if (ret) {
  2401. DRM_DEBUG_KMS("Invalid mode\n");
  2402. goto out;
  2403. }
  2404. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  2405. ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
  2406. mode, fb);
  2407. if (ret)
  2408. goto out;
  2409. }
  2410. if (crtc_req->count_connectors == 0 && mode) {
  2411. DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
  2412. ret = -EINVAL;
  2413. goto out;
  2414. }
  2415. if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  2416. DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
  2417. crtc_req->count_connectors);
  2418. ret = -EINVAL;
  2419. goto out;
  2420. }
  2421. if (crtc_req->count_connectors > 0) {
  2422. u32 out_id;
  2423. /* Avoid unbounded kernel memory allocation */
  2424. if (crtc_req->count_connectors > config->num_connector) {
  2425. ret = -EINVAL;
  2426. goto out;
  2427. }
  2428. connector_set = kmalloc_array(crtc_req->count_connectors,
  2429. sizeof(struct drm_connector *),
  2430. GFP_KERNEL);
  2431. if (!connector_set) {
  2432. ret = -ENOMEM;
  2433. goto out;
  2434. }
  2435. for (i = 0; i < crtc_req->count_connectors; i++) {
  2436. set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
  2437. if (get_user(out_id, &set_connectors_ptr[i])) {
  2438. ret = -EFAULT;
  2439. goto out;
  2440. }
  2441. connector = drm_connector_find(dev, out_id);
  2442. if (!connector) {
  2443. DRM_DEBUG_KMS("Connector id %d unknown\n",
  2444. out_id);
  2445. ret = -ENOENT;
  2446. goto out;
  2447. }
  2448. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  2449. connector->base.id,
  2450. connector->name);
  2451. connector_set[i] = connector;
  2452. }
  2453. }
  2454. set.crtc = crtc;
  2455. set.x = crtc_req->x;
  2456. set.y = crtc_req->y;
  2457. set.mode = mode;
  2458. set.connectors = connector_set;
  2459. set.num_connectors = crtc_req->count_connectors;
  2460. set.fb = fb;
  2461. ret = drm_mode_set_config_internal(&set);
  2462. out:
  2463. if (fb)
  2464. drm_framebuffer_unreference(fb);
  2465. kfree(connector_set);
  2466. drm_mode_destroy(dev, mode);
  2467. drm_modeset_unlock_all(dev);
  2468. return ret;
  2469. }
  2470. /**
  2471. * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
  2472. * universal plane handler call
  2473. * @crtc: crtc to update cursor for
  2474. * @req: data pointer for the ioctl
  2475. * @file_priv: drm file for the ioctl call
  2476. *
  2477. * Legacy cursor ioctl's work directly with driver buffer handles. To
  2478. * translate legacy ioctl calls into universal plane handler calls, we need to
  2479. * wrap the native buffer handle in a drm_framebuffer.
  2480. *
  2481. * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
  2482. * buffer with a pitch of 4*width; the universal plane interface should be used
  2483. * directly in cases where the hardware can support other buffer settings and
  2484. * userspace wants to make use of these capabilities.
  2485. *
  2486. * Returns:
  2487. * Zero on success, negative errno on failure.
  2488. */
  2489. static int drm_mode_cursor_universal(struct drm_crtc *crtc,
  2490. struct drm_mode_cursor2 *req,
  2491. struct drm_file *file_priv)
  2492. {
  2493. struct drm_device *dev = crtc->dev;
  2494. struct drm_framebuffer *fb = NULL;
  2495. struct drm_mode_fb_cmd2 fbreq = {
  2496. .width = req->width,
  2497. .height = req->height,
  2498. .pixel_format = DRM_FORMAT_ARGB8888,
  2499. .pitches = { req->width * 4 },
  2500. .handles = { req->handle },
  2501. };
  2502. int32_t crtc_x, crtc_y;
  2503. uint32_t crtc_w = 0, crtc_h = 0;
  2504. uint32_t src_w = 0, src_h = 0;
  2505. int ret = 0;
  2506. BUG_ON(!crtc->cursor);
  2507. WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
  2508. /*
  2509. * Obtain fb we'll be using (either new or existing) and take an extra
  2510. * reference to it if fb != null. setplane will take care of dropping
  2511. * the reference if the plane update fails.
  2512. */
  2513. if (req->flags & DRM_MODE_CURSOR_BO) {
  2514. if (req->handle) {
  2515. fb = add_framebuffer_internal(dev, &fbreq, file_priv);
  2516. if (IS_ERR(fb)) {
  2517. DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
  2518. return PTR_ERR(fb);
  2519. }
  2520. drm_framebuffer_reference(fb);
  2521. } else {
  2522. fb = NULL;
  2523. }
  2524. } else {
  2525. fb = crtc->cursor->fb;
  2526. if (fb)
  2527. drm_framebuffer_reference(fb);
  2528. }
  2529. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  2530. crtc_x = req->x;
  2531. crtc_y = req->y;
  2532. } else {
  2533. crtc_x = crtc->cursor_x;
  2534. crtc_y = crtc->cursor_y;
  2535. }
  2536. if (fb) {
  2537. crtc_w = fb->width;
  2538. crtc_h = fb->height;
  2539. src_w = fb->width << 16;
  2540. src_h = fb->height << 16;
  2541. }
  2542. /*
  2543. * setplane_internal will take care of deref'ing either the old or new
  2544. * framebuffer depending on success.
  2545. */
  2546. ret = __setplane_internal(crtc->cursor, crtc, fb,
  2547. crtc_x, crtc_y, crtc_w, crtc_h,
  2548. 0, 0, src_w, src_h);
  2549. /* Update successful; save new cursor position, if necessary */
  2550. if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
  2551. crtc->cursor_x = req->x;
  2552. crtc->cursor_y = req->y;
  2553. }
  2554. return ret;
  2555. }
  2556. static int drm_mode_cursor_common(struct drm_device *dev,
  2557. struct drm_mode_cursor2 *req,
  2558. struct drm_file *file_priv)
  2559. {
  2560. struct drm_crtc *crtc;
  2561. int ret = 0;
  2562. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2563. return -EINVAL;
  2564. if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
  2565. return -EINVAL;
  2566. crtc = drm_crtc_find(dev, req->crtc_id);
  2567. if (!crtc) {
  2568. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  2569. return -ENOENT;
  2570. }
  2571. /*
  2572. * If this crtc has a universal cursor plane, call that plane's update
  2573. * handler rather than using legacy cursor handlers.
  2574. */
  2575. drm_modeset_lock_crtc(crtc, crtc->cursor);
  2576. if (crtc->cursor) {
  2577. ret = drm_mode_cursor_universal(crtc, req, file_priv);
  2578. goto out;
  2579. }
  2580. if (req->flags & DRM_MODE_CURSOR_BO) {
  2581. if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
  2582. ret = -ENXIO;
  2583. goto out;
  2584. }
  2585. /* Turns off the cursor if handle is 0 */
  2586. if (crtc->funcs->cursor_set2)
  2587. ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
  2588. req->width, req->height, req->hot_x, req->hot_y);
  2589. else
  2590. ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  2591. req->width, req->height);
  2592. }
  2593. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  2594. if (crtc->funcs->cursor_move) {
  2595. ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  2596. } else {
  2597. ret = -EFAULT;
  2598. goto out;
  2599. }
  2600. }
  2601. out:
  2602. drm_modeset_unlock_crtc(crtc);
  2603. return ret;
  2604. }
  2605. /**
  2606. * drm_mode_cursor_ioctl - set CRTC's cursor configuration
  2607. * @dev: drm device for the ioctl
  2608. * @data: data pointer for the ioctl
  2609. * @file_priv: drm file for the ioctl call
  2610. *
  2611. * Set the cursor configuration based on user request.
  2612. *
  2613. * Called by the user via ioctl.
  2614. *
  2615. * Returns:
  2616. * Zero on success, negative errno on failure.
  2617. */
  2618. int drm_mode_cursor_ioctl(struct drm_device *dev,
  2619. void *data, struct drm_file *file_priv)
  2620. {
  2621. struct drm_mode_cursor *req = data;
  2622. struct drm_mode_cursor2 new_req;
  2623. memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
  2624. new_req.hot_x = new_req.hot_y = 0;
  2625. return drm_mode_cursor_common(dev, &new_req, file_priv);
  2626. }
  2627. /**
  2628. * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
  2629. * @dev: drm device for the ioctl
  2630. * @data: data pointer for the ioctl
  2631. * @file_priv: drm file for the ioctl call
  2632. *
  2633. * Set the cursor configuration based on user request. This implements the 2nd
  2634. * version of the cursor ioctl, which allows userspace to additionally specify
  2635. * the hotspot of the pointer.
  2636. *
  2637. * Called by the user via ioctl.
  2638. *
  2639. * Returns:
  2640. * Zero on success, negative errno on failure.
  2641. */
  2642. int drm_mode_cursor2_ioctl(struct drm_device *dev,
  2643. void *data, struct drm_file *file_priv)
  2644. {
  2645. struct drm_mode_cursor2 *req = data;
  2646. return drm_mode_cursor_common(dev, req, file_priv);
  2647. }
  2648. /**
  2649. * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
  2650. * @bpp: bits per pixels
  2651. * @depth: bit depth per pixel
  2652. *
  2653. * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
  2654. * Useful in fbdev emulation code, since that deals in those values.
  2655. */
  2656. uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
  2657. {
  2658. uint32_t fmt;
  2659. switch (bpp) {
  2660. case 8:
  2661. fmt = DRM_FORMAT_C8;
  2662. break;
  2663. case 16:
  2664. if (depth == 15)
  2665. fmt = DRM_FORMAT_XRGB1555;
  2666. else
  2667. fmt = DRM_FORMAT_RGB565;
  2668. break;
  2669. case 24:
  2670. fmt = DRM_FORMAT_RGB888;
  2671. break;
  2672. case 32:
  2673. if (depth == 24)
  2674. fmt = DRM_FORMAT_XRGB8888;
  2675. else if (depth == 30)
  2676. fmt = DRM_FORMAT_XRGB2101010;
  2677. else
  2678. fmt = DRM_FORMAT_ARGB8888;
  2679. break;
  2680. default:
  2681. DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
  2682. fmt = DRM_FORMAT_XRGB8888;
  2683. break;
  2684. }
  2685. return fmt;
  2686. }
  2687. EXPORT_SYMBOL(drm_mode_legacy_fb_format);
  2688. /**
  2689. * drm_mode_addfb - add an FB to the graphics configuration
  2690. * @dev: drm device for the ioctl
  2691. * @data: data pointer for the ioctl
  2692. * @file_priv: drm file for the ioctl call
  2693. *
  2694. * Add a new FB to the specified CRTC, given a user request. This is the
  2695. * original addfb ioctl which only supported RGB formats.
  2696. *
  2697. * Called by the user via ioctl.
  2698. *
  2699. * Returns:
  2700. * Zero on success, negative errno on failure.
  2701. */
  2702. int drm_mode_addfb(struct drm_device *dev,
  2703. void *data, struct drm_file *file_priv)
  2704. {
  2705. struct drm_mode_fb_cmd *or = data;
  2706. struct drm_mode_fb_cmd2 r = {};
  2707. int ret;
  2708. /* convert to new format and call new ioctl */
  2709. r.fb_id = or->fb_id;
  2710. r.width = or->width;
  2711. r.height = or->height;
  2712. r.pitches[0] = or->pitch;
  2713. r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
  2714. r.handles[0] = or->handle;
  2715. ret = drm_mode_addfb2(dev, &r, file_priv);
  2716. if (ret)
  2717. return ret;
  2718. or->fb_id = r.fb_id;
  2719. return 0;
  2720. }
  2721. static int format_check(const struct drm_mode_fb_cmd2 *r)
  2722. {
  2723. uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
  2724. switch (format) {
  2725. case DRM_FORMAT_C8:
  2726. case DRM_FORMAT_RGB332:
  2727. case DRM_FORMAT_BGR233:
  2728. case DRM_FORMAT_XRGB4444:
  2729. case DRM_FORMAT_XBGR4444:
  2730. case DRM_FORMAT_RGBX4444:
  2731. case DRM_FORMAT_BGRX4444:
  2732. case DRM_FORMAT_ARGB4444:
  2733. case DRM_FORMAT_ABGR4444:
  2734. case DRM_FORMAT_RGBA4444:
  2735. case DRM_FORMAT_BGRA4444:
  2736. case DRM_FORMAT_XRGB1555:
  2737. case DRM_FORMAT_XBGR1555:
  2738. case DRM_FORMAT_RGBX5551:
  2739. case DRM_FORMAT_BGRX5551:
  2740. case DRM_FORMAT_ARGB1555:
  2741. case DRM_FORMAT_ABGR1555:
  2742. case DRM_FORMAT_RGBA5551:
  2743. case DRM_FORMAT_BGRA5551:
  2744. case DRM_FORMAT_RGB565:
  2745. case DRM_FORMAT_BGR565:
  2746. case DRM_FORMAT_RGB888:
  2747. case DRM_FORMAT_BGR888:
  2748. case DRM_FORMAT_XRGB8888:
  2749. case DRM_FORMAT_XBGR8888:
  2750. case DRM_FORMAT_RGBX8888:
  2751. case DRM_FORMAT_BGRX8888:
  2752. case DRM_FORMAT_ARGB8888:
  2753. case DRM_FORMAT_ABGR8888:
  2754. case DRM_FORMAT_RGBA8888:
  2755. case DRM_FORMAT_BGRA8888:
  2756. case DRM_FORMAT_XRGB2101010:
  2757. case DRM_FORMAT_XBGR2101010:
  2758. case DRM_FORMAT_RGBX1010102:
  2759. case DRM_FORMAT_BGRX1010102:
  2760. case DRM_FORMAT_ARGB2101010:
  2761. case DRM_FORMAT_ABGR2101010:
  2762. case DRM_FORMAT_RGBA1010102:
  2763. case DRM_FORMAT_BGRA1010102:
  2764. case DRM_FORMAT_YUYV:
  2765. case DRM_FORMAT_YVYU:
  2766. case DRM_FORMAT_UYVY:
  2767. case DRM_FORMAT_VYUY:
  2768. case DRM_FORMAT_AYUV:
  2769. case DRM_FORMAT_NV12:
  2770. case DRM_FORMAT_NV21:
  2771. case DRM_FORMAT_NV16:
  2772. case DRM_FORMAT_NV61:
  2773. case DRM_FORMAT_NV24:
  2774. case DRM_FORMAT_NV42:
  2775. case DRM_FORMAT_YUV410:
  2776. case DRM_FORMAT_YVU410:
  2777. case DRM_FORMAT_YUV411:
  2778. case DRM_FORMAT_YVU411:
  2779. case DRM_FORMAT_YUV420:
  2780. case DRM_FORMAT_YVU420:
  2781. case DRM_FORMAT_YUV422:
  2782. case DRM_FORMAT_YVU422:
  2783. case DRM_FORMAT_YUV444:
  2784. case DRM_FORMAT_YVU444:
  2785. return 0;
  2786. default:
  2787. DRM_DEBUG_KMS("invalid pixel format %s\n",
  2788. drm_get_format_name(r->pixel_format));
  2789. return -EINVAL;
  2790. }
  2791. }
  2792. static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
  2793. {
  2794. int ret, hsub, vsub, num_planes, i;
  2795. ret = format_check(r);
  2796. if (ret) {
  2797. DRM_DEBUG_KMS("bad framebuffer format %s\n",
  2798. drm_get_format_name(r->pixel_format));
  2799. return ret;
  2800. }
  2801. hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
  2802. vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
  2803. num_planes = drm_format_num_planes(r->pixel_format);
  2804. if (r->width == 0 || r->width % hsub) {
  2805. DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
  2806. return -EINVAL;
  2807. }
  2808. if (r->height == 0 || r->height % vsub) {
  2809. DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
  2810. return -EINVAL;
  2811. }
  2812. for (i = 0; i < num_planes; i++) {
  2813. unsigned int width = r->width / (i != 0 ? hsub : 1);
  2814. unsigned int height = r->height / (i != 0 ? vsub : 1);
  2815. unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
  2816. if (!r->handles[i]) {
  2817. DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
  2818. return -EINVAL;
  2819. }
  2820. if ((uint64_t) width * cpp > UINT_MAX)
  2821. return -ERANGE;
  2822. if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
  2823. return -ERANGE;
  2824. if (r->pitches[i] < width * cpp) {
  2825. DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
  2826. return -EINVAL;
  2827. }
  2828. }
  2829. return 0;
  2830. }
  2831. static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
  2832. struct drm_mode_fb_cmd2 *r,
  2833. struct drm_file *file_priv)
  2834. {
  2835. struct drm_mode_config *config = &dev->mode_config;
  2836. struct drm_framebuffer *fb;
  2837. int ret;
  2838. if (r->flags & ~DRM_MODE_FB_INTERLACED) {
  2839. DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
  2840. return ERR_PTR(-EINVAL);
  2841. }
  2842. if ((config->min_width > r->width) || (r->width > config->max_width)) {
  2843. DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
  2844. r->width, config->min_width, config->max_width);
  2845. return ERR_PTR(-EINVAL);
  2846. }
  2847. if ((config->min_height > r->height) || (r->height > config->max_height)) {
  2848. DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
  2849. r->height, config->min_height, config->max_height);
  2850. return ERR_PTR(-EINVAL);
  2851. }
  2852. ret = framebuffer_check(r);
  2853. if (ret)
  2854. return ERR_PTR(ret);
  2855. fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
  2856. if (IS_ERR(fb)) {
  2857. DRM_DEBUG_KMS("could not create framebuffer\n");
  2858. return fb;
  2859. }
  2860. mutex_lock(&file_priv->fbs_lock);
  2861. r->fb_id = fb->base.id;
  2862. list_add(&fb->filp_head, &file_priv->fbs);
  2863. DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  2864. mutex_unlock(&file_priv->fbs_lock);
  2865. return fb;
  2866. }
  2867. /**
  2868. * drm_mode_addfb2 - add an FB to the graphics configuration
  2869. * @dev: drm device for the ioctl
  2870. * @data: data pointer for the ioctl
  2871. * @file_priv: drm file for the ioctl call
  2872. *
  2873. * Add a new FB to the specified CRTC, given a user request with format. This is
  2874. * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
  2875. * and uses fourcc codes as pixel format specifiers.
  2876. *
  2877. * Called by the user via ioctl.
  2878. *
  2879. * Returns:
  2880. * Zero on success, negative errno on failure.
  2881. */
  2882. int drm_mode_addfb2(struct drm_device *dev,
  2883. void *data, struct drm_file *file_priv)
  2884. {
  2885. struct drm_framebuffer *fb;
  2886. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2887. return -EINVAL;
  2888. fb = add_framebuffer_internal(dev, data, file_priv);
  2889. if (IS_ERR(fb))
  2890. return PTR_ERR(fb);
  2891. return 0;
  2892. }
  2893. /**
  2894. * drm_mode_rmfb - remove an FB from the configuration
  2895. * @dev: drm device for the ioctl
  2896. * @data: data pointer for the ioctl
  2897. * @file_priv: drm file for the ioctl call
  2898. *
  2899. * Remove the FB specified by the user.
  2900. *
  2901. * Called by the user via ioctl.
  2902. *
  2903. * Returns:
  2904. * Zero on success, negative errno on failure.
  2905. */
  2906. int drm_mode_rmfb(struct drm_device *dev,
  2907. void *data, struct drm_file *file_priv)
  2908. {
  2909. struct drm_framebuffer *fb = NULL;
  2910. struct drm_framebuffer *fbl = NULL;
  2911. uint32_t *id = data;
  2912. int found = 0;
  2913. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2914. return -EINVAL;
  2915. mutex_lock(&file_priv->fbs_lock);
  2916. mutex_lock(&dev->mode_config.fb_lock);
  2917. fb = __drm_framebuffer_lookup(dev, *id);
  2918. if (!fb)
  2919. goto fail_lookup;
  2920. list_for_each_entry(fbl, &file_priv->fbs, filp_head)
  2921. if (fb == fbl)
  2922. found = 1;
  2923. if (!found)
  2924. goto fail_lookup;
  2925. /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
  2926. __drm_framebuffer_unregister(dev, fb);
  2927. list_del_init(&fb->filp_head);
  2928. mutex_unlock(&dev->mode_config.fb_lock);
  2929. mutex_unlock(&file_priv->fbs_lock);
  2930. drm_framebuffer_remove(fb);
  2931. return 0;
  2932. fail_lookup:
  2933. mutex_unlock(&dev->mode_config.fb_lock);
  2934. mutex_unlock(&file_priv->fbs_lock);
  2935. return -ENOENT;
  2936. }
  2937. /**
  2938. * drm_mode_getfb - get FB info
  2939. * @dev: drm device for the ioctl
  2940. * @data: data pointer for the ioctl
  2941. * @file_priv: drm file for the ioctl call
  2942. *
  2943. * Lookup the FB given its ID and return info about it.
  2944. *
  2945. * Called by the user via ioctl.
  2946. *
  2947. * Returns:
  2948. * Zero on success, negative errno on failure.
  2949. */
  2950. int drm_mode_getfb(struct drm_device *dev,
  2951. void *data, struct drm_file *file_priv)
  2952. {
  2953. struct drm_mode_fb_cmd *r = data;
  2954. struct drm_framebuffer *fb;
  2955. int ret;
  2956. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2957. return -EINVAL;
  2958. fb = drm_framebuffer_lookup(dev, r->fb_id);
  2959. if (!fb)
  2960. return -ENOENT;
  2961. r->height = fb->height;
  2962. r->width = fb->width;
  2963. r->depth = fb->depth;
  2964. r->bpp = fb->bits_per_pixel;
  2965. r->pitch = fb->pitches[0];
  2966. if (fb->funcs->create_handle) {
  2967. if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
  2968. drm_is_control_client(file_priv)) {
  2969. ret = fb->funcs->create_handle(fb, file_priv,
  2970. &r->handle);
  2971. } else {
  2972. /* GET_FB() is an unprivileged ioctl so we must not
  2973. * return a buffer-handle to non-master processes! For
  2974. * backwards-compatibility reasons, we cannot make
  2975. * GET_FB() privileged, so just return an invalid handle
  2976. * for non-masters. */
  2977. r->handle = 0;
  2978. ret = 0;
  2979. }
  2980. } else {
  2981. ret = -ENODEV;
  2982. }
  2983. drm_framebuffer_unreference(fb);
  2984. return ret;
  2985. }
  2986. /**
  2987. * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
  2988. * @dev: drm device for the ioctl
  2989. * @data: data pointer for the ioctl
  2990. * @file_priv: drm file for the ioctl call
  2991. *
  2992. * Lookup the FB and flush out the damaged area supplied by userspace as a clip
  2993. * rectangle list. Generic userspace which does frontbuffer rendering must call
  2994. * this ioctl to flush out the changes on manual-update display outputs, e.g.
  2995. * usb display-link, mipi manual update panels or edp panel self refresh modes.
  2996. *
  2997. * Modesetting drivers which always update the frontbuffer do not need to
  2998. * implement the corresponding ->dirty framebuffer callback.
  2999. *
  3000. * Called by the user via ioctl.
  3001. *
  3002. * Returns:
  3003. * Zero on success, negative errno on failure.
  3004. */
  3005. int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
  3006. void *data, struct drm_file *file_priv)
  3007. {
  3008. struct drm_clip_rect __user *clips_ptr;
  3009. struct drm_clip_rect *clips = NULL;
  3010. struct drm_mode_fb_dirty_cmd *r = data;
  3011. struct drm_framebuffer *fb;
  3012. unsigned flags;
  3013. int num_clips;
  3014. int ret;
  3015. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3016. return -EINVAL;
  3017. fb = drm_framebuffer_lookup(dev, r->fb_id);
  3018. if (!fb)
  3019. return -ENOENT;
  3020. num_clips = r->num_clips;
  3021. clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
  3022. if (!num_clips != !clips_ptr) {
  3023. ret = -EINVAL;
  3024. goto out_err1;
  3025. }
  3026. flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
  3027. /* If userspace annotates copy, clips must come in pairs */
  3028. if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
  3029. ret = -EINVAL;
  3030. goto out_err1;
  3031. }
  3032. if (num_clips && clips_ptr) {
  3033. if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
  3034. ret = -EINVAL;
  3035. goto out_err1;
  3036. }
  3037. clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
  3038. if (!clips) {
  3039. ret = -ENOMEM;
  3040. goto out_err1;
  3041. }
  3042. ret = copy_from_user(clips, clips_ptr,
  3043. num_clips * sizeof(*clips));
  3044. if (ret) {
  3045. ret = -EFAULT;
  3046. goto out_err2;
  3047. }
  3048. }
  3049. if (fb->funcs->dirty) {
  3050. ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
  3051. clips, num_clips);
  3052. } else {
  3053. ret = -ENOSYS;
  3054. }
  3055. out_err2:
  3056. kfree(clips);
  3057. out_err1:
  3058. drm_framebuffer_unreference(fb);
  3059. return ret;
  3060. }
  3061. /**
  3062. * drm_fb_release - remove and free the FBs on this file
  3063. * @priv: drm file for the ioctl
  3064. *
  3065. * Destroy all the FBs associated with @filp.
  3066. *
  3067. * Called by the user via ioctl.
  3068. *
  3069. * Returns:
  3070. * Zero on success, negative errno on failure.
  3071. */
  3072. void drm_fb_release(struct drm_file *priv)
  3073. {
  3074. struct drm_device *dev = priv->minor->dev;
  3075. struct drm_framebuffer *fb, *tfb;
  3076. /*
  3077. * When the file gets released that means no one else can access the fb
  3078. * list any more, so no need to grab fpriv->fbs_lock. And we need to
  3079. * avoid upsetting lockdep since the universal cursor code adds a
  3080. * framebuffer while holding mutex locks.
  3081. *
  3082. * Note that a real deadlock between fpriv->fbs_lock and the modeset
  3083. * locks is impossible here since no one else but this function can get
  3084. * at it any more.
  3085. */
  3086. list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
  3087. mutex_lock(&dev->mode_config.fb_lock);
  3088. /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
  3089. __drm_framebuffer_unregister(dev, fb);
  3090. mutex_unlock(&dev->mode_config.fb_lock);
  3091. list_del_init(&fb->filp_head);
  3092. /* This will also drop the fpriv->fbs reference. */
  3093. drm_framebuffer_remove(fb);
  3094. }
  3095. }
  3096. /**
  3097. * drm_property_create - create a new property type
  3098. * @dev: drm device
  3099. * @flags: flags specifying the property type
  3100. * @name: name of the property
  3101. * @num_values: number of pre-defined values
  3102. *
  3103. * This creates a new generic drm property which can then be attached to a drm
  3104. * object with drm_object_attach_property. The returned property object must be
  3105. * freed with drm_property_destroy.
  3106. *
  3107. * Note that the DRM core keeps a per-device list of properties and that, if
  3108. * drm_mode_config_cleanup() is called, it will destroy all properties created
  3109. * by the driver.
  3110. *
  3111. * Returns:
  3112. * A pointer to the newly created property on success, NULL on failure.
  3113. */
  3114. struct drm_property *drm_property_create(struct drm_device *dev, int flags,
  3115. const char *name, int num_values)
  3116. {
  3117. struct drm_property *property = NULL;
  3118. int ret;
  3119. property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
  3120. if (!property)
  3121. return NULL;
  3122. property->dev = dev;
  3123. if (num_values) {
  3124. property->values = kcalloc(num_values, sizeof(uint64_t),
  3125. GFP_KERNEL);
  3126. if (!property->values)
  3127. goto fail;
  3128. }
  3129. ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
  3130. if (ret)
  3131. goto fail;
  3132. property->flags = flags;
  3133. property->num_values = num_values;
  3134. INIT_LIST_HEAD(&property->enum_list);
  3135. if (name) {
  3136. strncpy(property->name, name, DRM_PROP_NAME_LEN);
  3137. property->name[DRM_PROP_NAME_LEN-1] = '\0';
  3138. }
  3139. list_add_tail(&property->head, &dev->mode_config.property_list);
  3140. WARN_ON(!drm_property_type_valid(property));
  3141. return property;
  3142. fail:
  3143. kfree(property->values);
  3144. kfree(property);
  3145. return NULL;
  3146. }
  3147. EXPORT_SYMBOL(drm_property_create);
  3148. /**
  3149. * drm_property_create_enum - create a new enumeration property type
  3150. * @dev: drm device
  3151. * @flags: flags specifying the property type
  3152. * @name: name of the property
  3153. * @props: enumeration lists with property values
  3154. * @num_values: number of pre-defined values
  3155. *
  3156. * This creates a new generic drm property which can then be attached to a drm
  3157. * object with drm_object_attach_property. The returned property object must be
  3158. * freed with drm_property_destroy.
  3159. *
  3160. * Userspace is only allowed to set one of the predefined values for enumeration
  3161. * properties.
  3162. *
  3163. * Returns:
  3164. * A pointer to the newly created property on success, NULL on failure.
  3165. */
  3166. struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
  3167. const char *name,
  3168. const struct drm_prop_enum_list *props,
  3169. int num_values)
  3170. {
  3171. struct drm_property *property;
  3172. int i, ret;
  3173. flags |= DRM_MODE_PROP_ENUM;
  3174. property = drm_property_create(dev, flags, name, num_values);
  3175. if (!property)
  3176. return NULL;
  3177. for (i = 0; i < num_values; i++) {
  3178. ret = drm_property_add_enum(property, i,
  3179. props[i].type,
  3180. props[i].name);
  3181. if (ret) {
  3182. drm_property_destroy(dev, property);
  3183. return NULL;
  3184. }
  3185. }
  3186. return property;
  3187. }
  3188. EXPORT_SYMBOL(drm_property_create_enum);
  3189. /**
  3190. * drm_property_create_bitmask - create a new bitmask property type
  3191. * @dev: drm device
  3192. * @flags: flags specifying the property type
  3193. * @name: name of the property
  3194. * @props: enumeration lists with property bitflags
  3195. * @num_props: size of the @props array
  3196. * @supported_bits: bitmask of all supported enumeration values
  3197. *
  3198. * This creates a new bitmask drm property which can then be attached to a drm
  3199. * object with drm_object_attach_property. The returned property object must be
  3200. * freed with drm_property_destroy.
  3201. *
  3202. * Compared to plain enumeration properties userspace is allowed to set any
  3203. * or'ed together combination of the predefined property bitflag values
  3204. *
  3205. * Returns:
  3206. * A pointer to the newly created property on success, NULL on failure.
  3207. */
  3208. struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
  3209. int flags, const char *name,
  3210. const struct drm_prop_enum_list *props,
  3211. int num_props,
  3212. uint64_t supported_bits)
  3213. {
  3214. struct drm_property *property;
  3215. int i, ret, index = 0;
  3216. int num_values = hweight64(supported_bits);
  3217. flags |= DRM_MODE_PROP_BITMASK;
  3218. property = drm_property_create(dev, flags, name, num_values);
  3219. if (!property)
  3220. return NULL;
  3221. for (i = 0; i < num_props; i++) {
  3222. if (!(supported_bits & (1ULL << props[i].type)))
  3223. continue;
  3224. if (WARN_ON(index >= num_values)) {
  3225. drm_property_destroy(dev, property);
  3226. return NULL;
  3227. }
  3228. ret = drm_property_add_enum(property, index++,
  3229. props[i].type,
  3230. props[i].name);
  3231. if (ret) {
  3232. drm_property_destroy(dev, property);
  3233. return NULL;
  3234. }
  3235. }
  3236. return property;
  3237. }
  3238. EXPORT_SYMBOL(drm_property_create_bitmask);
  3239. static struct drm_property *property_create_range(struct drm_device *dev,
  3240. int flags, const char *name,
  3241. uint64_t min, uint64_t max)
  3242. {
  3243. struct drm_property *property;
  3244. property = drm_property_create(dev, flags, name, 2);
  3245. if (!property)
  3246. return NULL;
  3247. property->values[0] = min;
  3248. property->values[1] = max;
  3249. return property;
  3250. }
  3251. /**
  3252. * drm_property_create_range - create a new ranged property type
  3253. * @dev: drm device
  3254. * @flags: flags specifying the property type
  3255. * @name: name of the property
  3256. * @min: minimum value of the property
  3257. * @max: maximum value of the property
  3258. *
  3259. * This creates a new generic drm property which can then be attached to a drm
  3260. * object with drm_object_attach_property. The returned property object must be
  3261. * freed with drm_property_destroy.
  3262. *
  3263. * Userspace is allowed to set any integer value in the (min, max) range
  3264. * inclusive.
  3265. *
  3266. * Returns:
  3267. * A pointer to the newly created property on success, NULL on failure.
  3268. */
  3269. struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
  3270. const char *name,
  3271. uint64_t min, uint64_t max)
  3272. {
  3273. return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
  3274. name, min, max);
  3275. }
  3276. EXPORT_SYMBOL(drm_property_create_range);
  3277. struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
  3278. int flags, const char *name,
  3279. int64_t min, int64_t max)
  3280. {
  3281. return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
  3282. name, I642U64(min), I642U64(max));
  3283. }
  3284. EXPORT_SYMBOL(drm_property_create_signed_range);
  3285. struct drm_property *drm_property_create_object(struct drm_device *dev,
  3286. int flags, const char *name, uint32_t type)
  3287. {
  3288. struct drm_property *property;
  3289. flags |= DRM_MODE_PROP_OBJECT;
  3290. property = drm_property_create(dev, flags, name, 1);
  3291. if (!property)
  3292. return NULL;
  3293. property->values[0] = type;
  3294. return property;
  3295. }
  3296. EXPORT_SYMBOL(drm_property_create_object);
  3297. /**
  3298. * drm_property_add_enum - add a possible value to an enumeration property
  3299. * @property: enumeration property to change
  3300. * @index: index of the new enumeration
  3301. * @value: value of the new enumeration
  3302. * @name: symbolic name of the new enumeration
  3303. *
  3304. * This functions adds enumerations to a property.
  3305. *
  3306. * It's use is deprecated, drivers should use one of the more specific helpers
  3307. * to directly create the property with all enumerations already attached.
  3308. *
  3309. * Returns:
  3310. * Zero on success, error code on failure.
  3311. */
  3312. int drm_property_add_enum(struct drm_property *property, int index,
  3313. uint64_t value, const char *name)
  3314. {
  3315. struct drm_property_enum *prop_enum;
  3316. if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3317. drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
  3318. return -EINVAL;
  3319. /*
  3320. * Bitmask enum properties have the additional constraint of values
  3321. * from 0 to 63
  3322. */
  3323. if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
  3324. (value > 63))
  3325. return -EINVAL;
  3326. if (!list_empty(&property->enum_list)) {
  3327. list_for_each_entry(prop_enum, &property->enum_list, head) {
  3328. if (prop_enum->value == value) {
  3329. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  3330. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  3331. return 0;
  3332. }
  3333. }
  3334. }
  3335. prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
  3336. if (!prop_enum)
  3337. return -ENOMEM;
  3338. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  3339. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  3340. prop_enum->value = value;
  3341. property->values[index] = value;
  3342. list_add_tail(&prop_enum->head, &property->enum_list);
  3343. return 0;
  3344. }
  3345. EXPORT_SYMBOL(drm_property_add_enum);
  3346. /**
  3347. * drm_property_destroy - destroy a drm property
  3348. * @dev: drm device
  3349. * @property: property to destry
  3350. *
  3351. * This function frees a property including any attached resources like
  3352. * enumeration values.
  3353. */
  3354. void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
  3355. {
  3356. struct drm_property_enum *prop_enum, *pt;
  3357. list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
  3358. list_del(&prop_enum->head);
  3359. kfree(prop_enum);
  3360. }
  3361. if (property->num_values)
  3362. kfree(property->values);
  3363. drm_mode_object_put(dev, &property->base);
  3364. list_del(&property->head);
  3365. kfree(property);
  3366. }
  3367. EXPORT_SYMBOL(drm_property_destroy);
  3368. /**
  3369. * drm_object_attach_property - attach a property to a modeset object
  3370. * @obj: drm modeset object
  3371. * @property: property to attach
  3372. * @init_val: initial value of the property
  3373. *
  3374. * This attaches the given property to the modeset object with the given initial
  3375. * value. Currently this function cannot fail since the properties are stored in
  3376. * a statically sized array.
  3377. */
  3378. void drm_object_attach_property(struct drm_mode_object *obj,
  3379. struct drm_property *property,
  3380. uint64_t init_val)
  3381. {
  3382. int count = obj->properties->count;
  3383. if (count == DRM_OBJECT_MAX_PROPERTY) {
  3384. WARN(1, "Failed to attach object property (type: 0x%x). Please "
  3385. "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
  3386. "you see this message on the same object type.\n",
  3387. obj->type);
  3388. return;
  3389. }
  3390. obj->properties->properties[count] = property;
  3391. obj->properties->values[count] = init_val;
  3392. obj->properties->count++;
  3393. if (property->flags & DRM_MODE_PROP_ATOMIC)
  3394. obj->properties->atomic_count++;
  3395. }
  3396. EXPORT_SYMBOL(drm_object_attach_property);
  3397. /**
  3398. * drm_object_property_set_value - set the value of a property
  3399. * @obj: drm mode object to set property value for
  3400. * @property: property to set
  3401. * @val: value the property should be set to
  3402. *
  3403. * This functions sets a given property on a given object. This function only
  3404. * changes the software state of the property, it does not call into the
  3405. * driver's ->set_property callback.
  3406. *
  3407. * Returns:
  3408. * Zero on success, error code on failure.
  3409. */
  3410. int drm_object_property_set_value(struct drm_mode_object *obj,
  3411. struct drm_property *property, uint64_t val)
  3412. {
  3413. int i;
  3414. for (i = 0; i < obj->properties->count; i++) {
  3415. if (obj->properties->properties[i] == property) {
  3416. obj->properties->values[i] = val;
  3417. return 0;
  3418. }
  3419. }
  3420. return -EINVAL;
  3421. }
  3422. EXPORT_SYMBOL(drm_object_property_set_value);
  3423. /**
  3424. * drm_object_property_get_value - retrieve the value of a property
  3425. * @obj: drm mode object to get property value from
  3426. * @property: property to retrieve
  3427. * @val: storage for the property value
  3428. *
  3429. * This function retrieves the softare state of the given property for the given
  3430. * property. Since there is no driver callback to retrieve the current property
  3431. * value this might be out of sync with the hardware, depending upon the driver
  3432. * and property.
  3433. *
  3434. * Returns:
  3435. * Zero on success, error code on failure.
  3436. */
  3437. int drm_object_property_get_value(struct drm_mode_object *obj,
  3438. struct drm_property *property, uint64_t *val)
  3439. {
  3440. int i;
  3441. /* read-only properties bypass atomic mechanism and still store
  3442. * their value in obj->properties->values[].. mostly to avoid
  3443. * having to deal w/ EDID and similar props in atomic paths:
  3444. */
  3445. if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
  3446. !(property->flags & DRM_MODE_PROP_IMMUTABLE))
  3447. return drm_atomic_get_property(obj, property, val);
  3448. for (i = 0; i < obj->properties->count; i++) {
  3449. if (obj->properties->properties[i] == property) {
  3450. *val = obj->properties->values[i];
  3451. return 0;
  3452. }
  3453. }
  3454. return -EINVAL;
  3455. }
  3456. EXPORT_SYMBOL(drm_object_property_get_value);
  3457. /**
  3458. * drm_mode_getproperty_ioctl - get the property metadata
  3459. * @dev: DRM device
  3460. * @data: ioctl data
  3461. * @file_priv: DRM file info
  3462. *
  3463. * This function retrieves the metadata for a given property, like the different
  3464. * possible values for an enum property or the limits for a range property.
  3465. *
  3466. * Blob properties are special
  3467. *
  3468. * Called by the user via ioctl.
  3469. *
  3470. * Returns:
  3471. * Zero on success, negative errno on failure.
  3472. */
  3473. int drm_mode_getproperty_ioctl(struct drm_device *dev,
  3474. void *data, struct drm_file *file_priv)
  3475. {
  3476. struct drm_mode_get_property *out_resp = data;
  3477. struct drm_property *property;
  3478. int enum_count = 0;
  3479. int value_count = 0;
  3480. int ret = 0, i;
  3481. int copied;
  3482. struct drm_property_enum *prop_enum;
  3483. struct drm_mode_property_enum __user *enum_ptr;
  3484. uint64_t __user *values_ptr;
  3485. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3486. return -EINVAL;
  3487. drm_modeset_lock_all(dev);
  3488. property = drm_property_find(dev, out_resp->prop_id);
  3489. if (!property) {
  3490. ret = -ENOENT;
  3491. goto done;
  3492. }
  3493. if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3494. drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  3495. list_for_each_entry(prop_enum, &property->enum_list, head)
  3496. enum_count++;
  3497. }
  3498. value_count = property->num_values;
  3499. strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
  3500. out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
  3501. out_resp->flags = property->flags;
  3502. if ((out_resp->count_values >= value_count) && value_count) {
  3503. values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
  3504. for (i = 0; i < value_count; i++) {
  3505. if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
  3506. ret = -EFAULT;
  3507. goto done;
  3508. }
  3509. }
  3510. }
  3511. out_resp->count_values = value_count;
  3512. if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3513. drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  3514. if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
  3515. copied = 0;
  3516. enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
  3517. list_for_each_entry(prop_enum, &property->enum_list, head) {
  3518. if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
  3519. ret = -EFAULT;
  3520. goto done;
  3521. }
  3522. if (copy_to_user(&enum_ptr[copied].name,
  3523. &prop_enum->name, DRM_PROP_NAME_LEN)) {
  3524. ret = -EFAULT;
  3525. goto done;
  3526. }
  3527. copied++;
  3528. }
  3529. }
  3530. out_resp->count_enum_blobs = enum_count;
  3531. }
  3532. /*
  3533. * NOTE: The idea seems to have been to use this to read all the blob
  3534. * property values. But nothing ever added them to the corresponding
  3535. * list, userspace always used the special-purpose get_blob ioctl to
  3536. * read the value for a blob property. It also doesn't make a lot of
  3537. * sense to return values here when everything else is just metadata for
  3538. * the property itself.
  3539. */
  3540. if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
  3541. out_resp->count_enum_blobs = 0;
  3542. done:
  3543. drm_modeset_unlock_all(dev);
  3544. return ret;
  3545. }
  3546. static struct drm_property_blob *
  3547. drm_property_create_blob(struct drm_device *dev, size_t length,
  3548. const void *data)
  3549. {
  3550. struct drm_property_blob *blob;
  3551. int ret;
  3552. if (!length || !data)
  3553. return NULL;
  3554. blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
  3555. if (!blob)
  3556. return NULL;
  3557. ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
  3558. if (ret) {
  3559. kfree(blob);
  3560. return NULL;
  3561. }
  3562. blob->length = length;
  3563. memcpy(blob->data, data, length);
  3564. list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
  3565. return blob;
  3566. }
  3567. static void drm_property_destroy_blob(struct drm_device *dev,
  3568. struct drm_property_blob *blob)
  3569. {
  3570. drm_mode_object_put(dev, &blob->base);
  3571. list_del(&blob->head);
  3572. kfree(blob);
  3573. }
  3574. /**
  3575. * drm_mode_getblob_ioctl - get the contents of a blob property value
  3576. * @dev: DRM device
  3577. * @data: ioctl data
  3578. * @file_priv: DRM file info
  3579. *
  3580. * This function retrieves the contents of a blob property. The value stored in
  3581. * an object's blob property is just a normal modeset object id.
  3582. *
  3583. * Called by the user via ioctl.
  3584. *
  3585. * Returns:
  3586. * Zero on success, negative errno on failure.
  3587. */
  3588. int drm_mode_getblob_ioctl(struct drm_device *dev,
  3589. void *data, struct drm_file *file_priv)
  3590. {
  3591. struct drm_mode_get_blob *out_resp = data;
  3592. struct drm_property_blob *blob;
  3593. int ret = 0;
  3594. void __user *blob_ptr;
  3595. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3596. return -EINVAL;
  3597. drm_modeset_lock_all(dev);
  3598. blob = drm_property_blob_find(dev, out_resp->blob_id);
  3599. if (!blob) {
  3600. ret = -ENOENT;
  3601. goto done;
  3602. }
  3603. if (out_resp->length == blob->length) {
  3604. blob_ptr = (void __user *)(unsigned long)out_resp->data;
  3605. if (copy_to_user(blob_ptr, blob->data, blob->length)) {
  3606. ret = -EFAULT;
  3607. goto done;
  3608. }
  3609. }
  3610. out_resp->length = blob->length;
  3611. done:
  3612. drm_modeset_unlock_all(dev);
  3613. return ret;
  3614. }
  3615. /**
  3616. * drm_mode_connector_set_path_property - set tile property on connector
  3617. * @connector: connector to set property on.
  3618. * @path: path to use for property.
  3619. *
  3620. * This creates a property to expose to userspace to specify a
  3621. * connector path. This is mainly used for DisplayPort MST where
  3622. * connectors have a topology and we want to allow userspace to give
  3623. * them more meaningful names.
  3624. *
  3625. * Returns:
  3626. * Zero on success, negative errno on failure.
  3627. */
  3628. int drm_mode_connector_set_path_property(struct drm_connector *connector,
  3629. const char *path)
  3630. {
  3631. struct drm_device *dev = connector->dev;
  3632. size_t size = strlen(path) + 1;
  3633. int ret;
  3634. connector->path_blob_ptr = drm_property_create_blob(connector->dev,
  3635. size, path);
  3636. if (!connector->path_blob_ptr)
  3637. return -EINVAL;
  3638. ret = drm_object_property_set_value(&connector->base,
  3639. dev->mode_config.path_property,
  3640. connector->path_blob_ptr->base.id);
  3641. return ret;
  3642. }
  3643. EXPORT_SYMBOL(drm_mode_connector_set_path_property);
  3644. /**
  3645. * drm_mode_connector_set_tile_property - set tile property on connector
  3646. * @connector: connector to set property on.
  3647. *
  3648. * This looks up the tile information for a connector, and creates a
  3649. * property for userspace to parse if it exists. The property is of
  3650. * the form of 8 integers using ':' as a separator.
  3651. *
  3652. * Returns:
  3653. * Zero on success, errno on failure.
  3654. */
  3655. int drm_mode_connector_set_tile_property(struct drm_connector *connector)
  3656. {
  3657. struct drm_device *dev = connector->dev;
  3658. int ret, size;
  3659. char tile[256];
  3660. if (connector->tile_blob_ptr)
  3661. drm_property_destroy_blob(dev, connector->tile_blob_ptr);
  3662. if (!connector->has_tile) {
  3663. connector->tile_blob_ptr = NULL;
  3664. ret = drm_object_property_set_value(&connector->base,
  3665. dev->mode_config.tile_property, 0);
  3666. return ret;
  3667. }
  3668. snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
  3669. connector->tile_group->id, connector->tile_is_single_monitor,
  3670. connector->num_h_tile, connector->num_v_tile,
  3671. connector->tile_h_loc, connector->tile_v_loc,
  3672. connector->tile_h_size, connector->tile_v_size);
  3673. size = strlen(tile) + 1;
  3674. connector->tile_blob_ptr = drm_property_create_blob(connector->dev,
  3675. size, tile);
  3676. if (!connector->tile_blob_ptr)
  3677. return -EINVAL;
  3678. ret = drm_object_property_set_value(&connector->base,
  3679. dev->mode_config.tile_property,
  3680. connector->tile_blob_ptr->base.id);
  3681. return ret;
  3682. }
  3683. EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
  3684. /**
  3685. * drm_mode_connector_update_edid_property - update the edid property of a connector
  3686. * @connector: drm connector
  3687. * @edid: new value of the edid property
  3688. *
  3689. * This function creates a new blob modeset object and assigns its id to the
  3690. * connector's edid property.
  3691. *
  3692. * Returns:
  3693. * Zero on success, negative errno on failure.
  3694. */
  3695. int drm_mode_connector_update_edid_property(struct drm_connector *connector,
  3696. const struct edid *edid)
  3697. {
  3698. struct drm_device *dev = connector->dev;
  3699. size_t size;
  3700. int ret;
  3701. /* ignore requests to set edid when overridden */
  3702. if (connector->override_edid)
  3703. return 0;
  3704. if (connector->edid_blob_ptr)
  3705. drm_property_destroy_blob(dev, connector->edid_blob_ptr);
  3706. /* Delete edid, when there is none. */
  3707. if (!edid) {
  3708. connector->edid_blob_ptr = NULL;
  3709. ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
  3710. return ret;
  3711. }
  3712. size = EDID_LENGTH * (1 + edid->extensions);
  3713. connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
  3714. size, edid);
  3715. if (!connector->edid_blob_ptr)
  3716. return -EINVAL;
  3717. ret = drm_object_property_set_value(&connector->base,
  3718. dev->mode_config.edid_property,
  3719. connector->edid_blob_ptr->base.id);
  3720. return ret;
  3721. }
  3722. EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
  3723. /* Some properties could refer to dynamic refcnt'd objects, or things that
  3724. * need special locking to handle lifetime issues (ie. to ensure the prop
  3725. * value doesn't become invalid part way through the property update due to
  3726. * race). The value returned by reference via 'obj' should be passed back
  3727. * to drm_property_change_valid_put() after the property is set (and the
  3728. * object to which the property is attached has a chance to take it's own
  3729. * reference).
  3730. */
  3731. static bool drm_property_change_valid_get(struct drm_property *property,
  3732. uint64_t value, struct drm_mode_object **ref)
  3733. {
  3734. int i;
  3735. if (property->flags & DRM_MODE_PROP_IMMUTABLE)
  3736. return false;
  3737. *ref = NULL;
  3738. if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
  3739. if (value < property->values[0] || value > property->values[1])
  3740. return false;
  3741. return true;
  3742. } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
  3743. int64_t svalue = U642I64(value);
  3744. if (svalue < U642I64(property->values[0]) ||
  3745. svalue > U642I64(property->values[1]))
  3746. return false;
  3747. return true;
  3748. } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  3749. uint64_t valid_mask = 0;
  3750. for (i = 0; i < property->num_values; i++)
  3751. valid_mask |= (1ULL << property->values[i]);
  3752. return !(value & ~valid_mask);
  3753. } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
  3754. /* Only the driver knows */
  3755. return true;
  3756. } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
  3757. /* a zero value for an object property translates to null: */
  3758. if (value == 0)
  3759. return true;
  3760. /* handle refcnt'd objects specially: */
  3761. if (property->values[0] == DRM_MODE_OBJECT_FB) {
  3762. struct drm_framebuffer *fb;
  3763. fb = drm_framebuffer_lookup(property->dev, value);
  3764. if (fb) {
  3765. *ref = &fb->base;
  3766. return true;
  3767. } else {
  3768. return false;
  3769. }
  3770. } else {
  3771. return _object_find(property->dev, value, property->values[0]) != NULL;
  3772. }
  3773. } else {
  3774. int i;
  3775. for (i = 0; i < property->num_values; i++)
  3776. if (property->values[i] == value)
  3777. return true;
  3778. return false;
  3779. }
  3780. for (i = 0; i < property->num_values; i++)
  3781. if (property->values[i] == value)
  3782. return true;
  3783. return false;
  3784. }
  3785. static void drm_property_change_valid_put(struct drm_property *property,
  3786. struct drm_mode_object *ref)
  3787. {
  3788. if (!ref)
  3789. return;
  3790. if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
  3791. if (property->values[0] == DRM_MODE_OBJECT_FB)
  3792. drm_framebuffer_unreference(obj_to_fb(ref));
  3793. }
  3794. }
  3795. /**
  3796. * drm_mode_connector_property_set_ioctl - set the current value of a connector property
  3797. * @dev: DRM device
  3798. * @data: ioctl data
  3799. * @file_priv: DRM file info
  3800. *
  3801. * This function sets the current value for a connectors's property. It also
  3802. * calls into a driver's ->set_property callback to update the hardware state
  3803. *
  3804. * Called by the user via ioctl.
  3805. *
  3806. * Returns:
  3807. * Zero on success, negative errno on failure.
  3808. */
  3809. int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
  3810. void *data, struct drm_file *file_priv)
  3811. {
  3812. struct drm_mode_connector_set_property *conn_set_prop = data;
  3813. struct drm_mode_obj_set_property obj_set_prop = {
  3814. .value = conn_set_prop->value,
  3815. .prop_id = conn_set_prop->prop_id,
  3816. .obj_id = conn_set_prop->connector_id,
  3817. .obj_type = DRM_MODE_OBJECT_CONNECTOR
  3818. };
  3819. /* It does all the locking and checking we need */
  3820. return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
  3821. }
  3822. static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
  3823. struct drm_property *property,
  3824. uint64_t value)
  3825. {
  3826. int ret = -EINVAL;
  3827. struct drm_connector *connector = obj_to_connector(obj);
  3828. /* Do DPMS ourselves */
  3829. if (property == connector->dev->mode_config.dpms_property) {
  3830. if (connector->funcs->dpms)
  3831. (*connector->funcs->dpms)(connector, (int)value);
  3832. ret = 0;
  3833. } else if (connector->funcs->set_property)
  3834. ret = connector->funcs->set_property(connector, property, value);
  3835. /* store the property value if successful */
  3836. if (!ret)
  3837. drm_object_property_set_value(&connector->base, property, value);
  3838. return ret;
  3839. }
  3840. static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
  3841. struct drm_property *property,
  3842. uint64_t value)
  3843. {
  3844. int ret = -EINVAL;
  3845. struct drm_crtc *crtc = obj_to_crtc(obj);
  3846. if (crtc->funcs->set_property)
  3847. ret = crtc->funcs->set_property(crtc, property, value);
  3848. if (!ret)
  3849. drm_object_property_set_value(obj, property, value);
  3850. return ret;
  3851. }
  3852. /**
  3853. * drm_mode_plane_set_obj_prop - set the value of a property
  3854. * @plane: drm plane object to set property value for
  3855. * @property: property to set
  3856. * @value: value the property should be set to
  3857. *
  3858. * This functions sets a given property on a given plane object. This function
  3859. * calls the driver's ->set_property callback and changes the software state of
  3860. * the property if the callback succeeds.
  3861. *
  3862. * Returns:
  3863. * Zero on success, error code on failure.
  3864. */
  3865. int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
  3866. struct drm_property *property,
  3867. uint64_t value)
  3868. {
  3869. int ret = -EINVAL;
  3870. struct drm_mode_object *obj = &plane->base;
  3871. if (plane->funcs->set_property)
  3872. ret = plane->funcs->set_property(plane, property, value);
  3873. if (!ret)
  3874. drm_object_property_set_value(obj, property, value);
  3875. return ret;
  3876. }
  3877. EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
  3878. /**
  3879. * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
  3880. * @dev: DRM device
  3881. * @data: ioctl data
  3882. * @file_priv: DRM file info
  3883. *
  3884. * This function retrieves the current value for an object's property. Compared
  3885. * to the connector specific ioctl this one is extended to also work on crtc and
  3886. * plane objects.
  3887. *
  3888. * Called by the user via ioctl.
  3889. *
  3890. * Returns:
  3891. * Zero on success, negative errno on failure.
  3892. */
  3893. int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
  3894. struct drm_file *file_priv)
  3895. {
  3896. struct drm_mode_obj_get_properties *arg = data;
  3897. struct drm_mode_object *obj;
  3898. int ret = 0;
  3899. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3900. return -EINVAL;
  3901. drm_modeset_lock_all(dev);
  3902. obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  3903. if (!obj) {
  3904. ret = -ENOENT;
  3905. goto out;
  3906. }
  3907. if (!obj->properties) {
  3908. ret = -EINVAL;
  3909. goto out;
  3910. }
  3911. ret = get_properties(obj, file_priv->atomic,
  3912. (uint32_t __user *)(unsigned long)(arg->props_ptr),
  3913. (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
  3914. &arg->count_props);
  3915. out:
  3916. drm_modeset_unlock_all(dev);
  3917. return ret;
  3918. }
  3919. /**
  3920. * drm_mode_obj_set_property_ioctl - set the current value of an object's property
  3921. * @dev: DRM device
  3922. * @data: ioctl data
  3923. * @file_priv: DRM file info
  3924. *
  3925. * This function sets the current value for an object's property. It also calls
  3926. * into a driver's ->set_property callback to update the hardware state.
  3927. * Compared to the connector specific ioctl this one is extended to also work on
  3928. * crtc and plane objects.
  3929. *
  3930. * Called by the user via ioctl.
  3931. *
  3932. * Returns:
  3933. * Zero on success, negative errno on failure.
  3934. */
  3935. int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
  3936. struct drm_file *file_priv)
  3937. {
  3938. struct drm_mode_obj_set_property *arg = data;
  3939. struct drm_mode_object *arg_obj;
  3940. struct drm_mode_object *prop_obj;
  3941. struct drm_property *property;
  3942. int i, ret = -EINVAL;
  3943. struct drm_mode_object *ref;
  3944. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3945. return -EINVAL;
  3946. drm_modeset_lock_all(dev);
  3947. arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  3948. if (!arg_obj) {
  3949. ret = -ENOENT;
  3950. goto out;
  3951. }
  3952. if (!arg_obj->properties)
  3953. goto out;
  3954. for (i = 0; i < arg_obj->properties->count; i++)
  3955. if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
  3956. break;
  3957. if (i == arg_obj->properties->count)
  3958. goto out;
  3959. prop_obj = drm_mode_object_find(dev, arg->prop_id,
  3960. DRM_MODE_OBJECT_PROPERTY);
  3961. if (!prop_obj) {
  3962. ret = -ENOENT;
  3963. goto out;
  3964. }
  3965. property = obj_to_property(prop_obj);
  3966. if (!drm_property_change_valid_get(property, arg->value, &ref))
  3967. goto out;
  3968. switch (arg_obj->type) {
  3969. case DRM_MODE_OBJECT_CONNECTOR:
  3970. ret = drm_mode_connector_set_obj_prop(arg_obj, property,
  3971. arg->value);
  3972. break;
  3973. case DRM_MODE_OBJECT_CRTC:
  3974. ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
  3975. break;
  3976. case DRM_MODE_OBJECT_PLANE:
  3977. ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
  3978. property, arg->value);
  3979. break;
  3980. }
  3981. drm_property_change_valid_put(property, ref);
  3982. out:
  3983. drm_modeset_unlock_all(dev);
  3984. return ret;
  3985. }
  3986. /**
  3987. * drm_mode_connector_attach_encoder - attach a connector to an encoder
  3988. * @connector: connector to attach
  3989. * @encoder: encoder to attach @connector to
  3990. *
  3991. * This function links up a connector to an encoder. Note that the routing
  3992. * restrictions between encoders and crtcs are exposed to userspace through the
  3993. * possible_clones and possible_crtcs bitmasks.
  3994. *
  3995. * Returns:
  3996. * Zero on success, negative errno on failure.
  3997. */
  3998. int drm_mode_connector_attach_encoder(struct drm_connector *connector,
  3999. struct drm_encoder *encoder)
  4000. {
  4001. int i;
  4002. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  4003. if (connector->encoder_ids[i] == 0) {
  4004. connector->encoder_ids[i] = encoder->base.id;
  4005. return 0;
  4006. }
  4007. }
  4008. return -ENOMEM;
  4009. }
  4010. EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
  4011. /**
  4012. * drm_mode_crtc_set_gamma_size - set the gamma table size
  4013. * @crtc: CRTC to set the gamma table size for
  4014. * @gamma_size: size of the gamma table
  4015. *
  4016. * Drivers which support gamma tables should set this to the supported gamma
  4017. * table size when initializing the CRTC. Currently the drm core only supports a
  4018. * fixed gamma table size.
  4019. *
  4020. * Returns:
  4021. * Zero on success, negative errno on failure.
  4022. */
  4023. int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
  4024. int gamma_size)
  4025. {
  4026. crtc->gamma_size = gamma_size;
  4027. crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
  4028. GFP_KERNEL);
  4029. if (!crtc->gamma_store) {
  4030. crtc->gamma_size = 0;
  4031. return -ENOMEM;
  4032. }
  4033. return 0;
  4034. }
  4035. EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
  4036. /**
  4037. * drm_mode_gamma_set_ioctl - set the gamma table
  4038. * @dev: DRM device
  4039. * @data: ioctl data
  4040. * @file_priv: DRM file info
  4041. *
  4042. * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
  4043. * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
  4044. *
  4045. * Called by the user via ioctl.
  4046. *
  4047. * Returns:
  4048. * Zero on success, negative errno on failure.
  4049. */
  4050. int drm_mode_gamma_set_ioctl(struct drm_device *dev,
  4051. void *data, struct drm_file *file_priv)
  4052. {
  4053. struct drm_mode_crtc_lut *crtc_lut = data;
  4054. struct drm_crtc *crtc;
  4055. void *r_base, *g_base, *b_base;
  4056. int size;
  4057. int ret = 0;
  4058. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4059. return -EINVAL;
  4060. drm_modeset_lock_all(dev);
  4061. crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
  4062. if (!crtc) {
  4063. ret = -ENOENT;
  4064. goto out;
  4065. }
  4066. if (crtc->funcs->gamma_set == NULL) {
  4067. ret = -ENOSYS;
  4068. goto out;
  4069. }
  4070. /* memcpy into gamma store */
  4071. if (crtc_lut->gamma_size != crtc->gamma_size) {
  4072. ret = -EINVAL;
  4073. goto out;
  4074. }
  4075. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  4076. r_base = crtc->gamma_store;
  4077. if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
  4078. ret = -EFAULT;
  4079. goto out;
  4080. }
  4081. g_base = r_base + size;
  4082. if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
  4083. ret = -EFAULT;
  4084. goto out;
  4085. }
  4086. b_base = g_base + size;
  4087. if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
  4088. ret = -EFAULT;
  4089. goto out;
  4090. }
  4091. crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
  4092. out:
  4093. drm_modeset_unlock_all(dev);
  4094. return ret;
  4095. }
  4096. /**
  4097. * drm_mode_gamma_get_ioctl - get the gamma table
  4098. * @dev: DRM device
  4099. * @data: ioctl data
  4100. * @file_priv: DRM file info
  4101. *
  4102. * Copy the current gamma table into the storage provided. This also provides
  4103. * the gamma table size the driver expects, which can be used to size the
  4104. * allocated storage.
  4105. *
  4106. * Called by the user via ioctl.
  4107. *
  4108. * Returns:
  4109. * Zero on success, negative errno on failure.
  4110. */
  4111. int drm_mode_gamma_get_ioctl(struct drm_device *dev,
  4112. void *data, struct drm_file *file_priv)
  4113. {
  4114. struct drm_mode_crtc_lut *crtc_lut = data;
  4115. struct drm_crtc *crtc;
  4116. void *r_base, *g_base, *b_base;
  4117. int size;
  4118. int ret = 0;
  4119. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4120. return -EINVAL;
  4121. drm_modeset_lock_all(dev);
  4122. crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
  4123. if (!crtc) {
  4124. ret = -ENOENT;
  4125. goto out;
  4126. }
  4127. /* memcpy into gamma store */
  4128. if (crtc_lut->gamma_size != crtc->gamma_size) {
  4129. ret = -EINVAL;
  4130. goto out;
  4131. }
  4132. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  4133. r_base = crtc->gamma_store;
  4134. if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
  4135. ret = -EFAULT;
  4136. goto out;
  4137. }
  4138. g_base = r_base + size;
  4139. if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
  4140. ret = -EFAULT;
  4141. goto out;
  4142. }
  4143. b_base = g_base + size;
  4144. if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
  4145. ret = -EFAULT;
  4146. goto out;
  4147. }
  4148. out:
  4149. drm_modeset_unlock_all(dev);
  4150. return ret;
  4151. }
  4152. /**
  4153. * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
  4154. * @dev: DRM device
  4155. * @data: ioctl data
  4156. * @file_priv: DRM file info
  4157. *
  4158. * This schedules an asynchronous update on a given CRTC, called page flip.
  4159. * Optionally a drm event is generated to signal the completion of the event.
  4160. * Generic drivers cannot assume that a pageflip with changed framebuffer
  4161. * properties (including driver specific metadata like tiling layout) will work,
  4162. * but some drivers support e.g. pixel format changes through the pageflip
  4163. * ioctl.
  4164. *
  4165. * Called by the user via ioctl.
  4166. *
  4167. * Returns:
  4168. * Zero on success, negative errno on failure.
  4169. */
  4170. int drm_mode_page_flip_ioctl(struct drm_device *dev,
  4171. void *data, struct drm_file *file_priv)
  4172. {
  4173. struct drm_mode_crtc_page_flip *page_flip = data;
  4174. struct drm_crtc *crtc;
  4175. struct drm_framebuffer *fb = NULL;
  4176. struct drm_pending_vblank_event *e = NULL;
  4177. unsigned long flags;
  4178. int ret = -EINVAL;
  4179. if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
  4180. page_flip->reserved != 0)
  4181. return -EINVAL;
  4182. if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
  4183. return -EINVAL;
  4184. crtc = drm_crtc_find(dev, page_flip->crtc_id);
  4185. if (!crtc)
  4186. return -ENOENT;
  4187. drm_modeset_lock_crtc(crtc, crtc->primary);
  4188. if (crtc->primary->fb == NULL) {
  4189. /* The framebuffer is currently unbound, presumably
  4190. * due to a hotplug event, that userspace has not
  4191. * yet discovered.
  4192. */
  4193. ret = -EBUSY;
  4194. goto out;
  4195. }
  4196. if (crtc->funcs->page_flip == NULL)
  4197. goto out;
  4198. fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
  4199. if (!fb) {
  4200. ret = -ENOENT;
  4201. goto out;
  4202. }
  4203. ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
  4204. if (ret)
  4205. goto out;
  4206. if (crtc->primary->fb->pixel_format != fb->pixel_format) {
  4207. DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
  4208. ret = -EINVAL;
  4209. goto out;
  4210. }
  4211. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  4212. ret = -ENOMEM;
  4213. spin_lock_irqsave(&dev->event_lock, flags);
  4214. if (file_priv->event_space < sizeof(e->event)) {
  4215. spin_unlock_irqrestore(&dev->event_lock, flags);
  4216. goto out;
  4217. }
  4218. file_priv->event_space -= sizeof(e->event);
  4219. spin_unlock_irqrestore(&dev->event_lock, flags);
  4220. e = kzalloc(sizeof(*e), GFP_KERNEL);
  4221. if (e == NULL) {
  4222. spin_lock_irqsave(&dev->event_lock, flags);
  4223. file_priv->event_space += sizeof(e->event);
  4224. spin_unlock_irqrestore(&dev->event_lock, flags);
  4225. goto out;
  4226. }
  4227. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  4228. e->event.base.length = sizeof(e->event);
  4229. e->event.user_data = page_flip->user_data;
  4230. e->base.event = &e->event.base;
  4231. e->base.file_priv = file_priv;
  4232. e->base.destroy =
  4233. (void (*) (struct drm_pending_event *)) kfree;
  4234. }
  4235. crtc->primary->old_fb = crtc->primary->fb;
  4236. ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
  4237. if (ret) {
  4238. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  4239. spin_lock_irqsave(&dev->event_lock, flags);
  4240. file_priv->event_space += sizeof(e->event);
  4241. spin_unlock_irqrestore(&dev->event_lock, flags);
  4242. kfree(e);
  4243. }
  4244. /* Keep the old fb, don't unref it. */
  4245. crtc->primary->old_fb = NULL;
  4246. } else {
  4247. /*
  4248. * Warn if the driver hasn't properly updated the crtc->fb
  4249. * field to reflect that the new framebuffer is now used.
  4250. * Failing to do so will screw with the reference counting
  4251. * on framebuffers.
  4252. */
  4253. WARN_ON(crtc->primary->fb != fb);
  4254. /* Unref only the old framebuffer. */
  4255. fb = NULL;
  4256. }
  4257. out:
  4258. if (fb)
  4259. drm_framebuffer_unreference(fb);
  4260. if (crtc->primary->old_fb)
  4261. drm_framebuffer_unreference(crtc->primary->old_fb);
  4262. crtc->primary->old_fb = NULL;
  4263. drm_modeset_unlock_crtc(crtc);
  4264. return ret;
  4265. }
  4266. /**
  4267. * drm_mode_config_reset - call ->reset callbacks
  4268. * @dev: drm device
  4269. *
  4270. * This functions calls all the crtc's, encoder's and connector's ->reset
  4271. * callback. Drivers can use this in e.g. their driver load or resume code to
  4272. * reset hardware and software state.
  4273. */
  4274. void drm_mode_config_reset(struct drm_device *dev)
  4275. {
  4276. struct drm_crtc *crtc;
  4277. struct drm_plane *plane;
  4278. struct drm_encoder *encoder;
  4279. struct drm_connector *connector;
  4280. list_for_each_entry(plane, &dev->mode_config.plane_list, head)
  4281. if (plane->funcs->reset)
  4282. plane->funcs->reset(plane);
  4283. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  4284. if (crtc->funcs->reset)
  4285. crtc->funcs->reset(crtc);
  4286. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  4287. if (encoder->funcs->reset)
  4288. encoder->funcs->reset(encoder);
  4289. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4290. connector->status = connector_status_unknown;
  4291. if (connector->funcs->reset)
  4292. connector->funcs->reset(connector);
  4293. }
  4294. }
  4295. EXPORT_SYMBOL(drm_mode_config_reset);
  4296. /**
  4297. * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
  4298. * @dev: DRM device
  4299. * @data: ioctl data
  4300. * @file_priv: DRM file info
  4301. *
  4302. * This creates a new dumb buffer in the driver's backing storage manager (GEM,
  4303. * TTM or something else entirely) and returns the resulting buffer handle. This
  4304. * handle can then be wrapped up into a framebuffer modeset object.
  4305. *
  4306. * Note that userspace is not allowed to use such objects for render
  4307. * acceleration - drivers must create their own private ioctls for such a use
  4308. * case.
  4309. *
  4310. * Called by the user via ioctl.
  4311. *
  4312. * Returns:
  4313. * Zero on success, negative errno on failure.
  4314. */
  4315. int drm_mode_create_dumb_ioctl(struct drm_device *dev,
  4316. void *data, struct drm_file *file_priv)
  4317. {
  4318. struct drm_mode_create_dumb *args = data;
  4319. u32 cpp, stride, size;
  4320. if (!dev->driver->dumb_create)
  4321. return -ENOSYS;
  4322. if (!args->width || !args->height || !args->bpp)
  4323. return -EINVAL;
  4324. /* overflow checks for 32bit size calculations */
  4325. /* NOTE: DIV_ROUND_UP() can overflow */
  4326. cpp = DIV_ROUND_UP(args->bpp, 8);
  4327. if (!cpp || cpp > 0xffffffffU / args->width)
  4328. return -EINVAL;
  4329. stride = cpp * args->width;
  4330. if (args->height > 0xffffffffU / stride)
  4331. return -EINVAL;
  4332. /* test for wrap-around */
  4333. size = args->height * stride;
  4334. if (PAGE_ALIGN(size) == 0)
  4335. return -EINVAL;
  4336. /*
  4337. * handle, pitch and size are output parameters. Zero them out to
  4338. * prevent drivers from accidentally using uninitialized data. Since
  4339. * not all existing userspace is clearing these fields properly we
  4340. * cannot reject IOCTL with garbage in them.
  4341. */
  4342. args->handle = 0;
  4343. args->pitch = 0;
  4344. args->size = 0;
  4345. return dev->driver->dumb_create(file_priv, dev, args);
  4346. }
  4347. /**
  4348. * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
  4349. * @dev: DRM device
  4350. * @data: ioctl data
  4351. * @file_priv: DRM file info
  4352. *
  4353. * Allocate an offset in the drm device node's address space to be able to
  4354. * memory map a dumb buffer.
  4355. *
  4356. * Called by the user via ioctl.
  4357. *
  4358. * Returns:
  4359. * Zero on success, negative errno on failure.
  4360. */
  4361. int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
  4362. void *data, struct drm_file *file_priv)
  4363. {
  4364. struct drm_mode_map_dumb *args = data;
  4365. /* call driver ioctl to get mmap offset */
  4366. if (!dev->driver->dumb_map_offset)
  4367. return -ENOSYS;
  4368. return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
  4369. }
  4370. /**
  4371. * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
  4372. * @dev: DRM device
  4373. * @data: ioctl data
  4374. * @file_priv: DRM file info
  4375. *
  4376. * This destroys the userspace handle for the given dumb backing storage buffer.
  4377. * Since buffer objects must be reference counted in the kernel a buffer object
  4378. * won't be immediately freed if a framebuffer modeset object still uses it.
  4379. *
  4380. * Called by the user via ioctl.
  4381. *
  4382. * Returns:
  4383. * Zero on success, negative errno on failure.
  4384. */
  4385. int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
  4386. void *data, struct drm_file *file_priv)
  4387. {
  4388. struct drm_mode_destroy_dumb *args = data;
  4389. if (!dev->driver->dumb_destroy)
  4390. return -ENOSYS;
  4391. return dev->driver->dumb_destroy(file_priv, dev, args->handle);
  4392. }
  4393. /**
  4394. * drm_fb_get_bpp_depth - get the bpp/depth values for format
  4395. * @format: pixel format (DRM_FORMAT_*)
  4396. * @depth: storage for the depth value
  4397. * @bpp: storage for the bpp value
  4398. *
  4399. * This only supports RGB formats here for compat with code that doesn't use
  4400. * pixel formats directly yet.
  4401. */
  4402. void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
  4403. int *bpp)
  4404. {
  4405. switch (format) {
  4406. case DRM_FORMAT_C8:
  4407. case DRM_FORMAT_RGB332:
  4408. case DRM_FORMAT_BGR233:
  4409. *depth = 8;
  4410. *bpp = 8;
  4411. break;
  4412. case DRM_FORMAT_XRGB1555:
  4413. case DRM_FORMAT_XBGR1555:
  4414. case DRM_FORMAT_RGBX5551:
  4415. case DRM_FORMAT_BGRX5551:
  4416. case DRM_FORMAT_ARGB1555:
  4417. case DRM_FORMAT_ABGR1555:
  4418. case DRM_FORMAT_RGBA5551:
  4419. case DRM_FORMAT_BGRA5551:
  4420. *depth = 15;
  4421. *bpp = 16;
  4422. break;
  4423. case DRM_FORMAT_RGB565:
  4424. case DRM_FORMAT_BGR565:
  4425. *depth = 16;
  4426. *bpp = 16;
  4427. break;
  4428. case DRM_FORMAT_RGB888:
  4429. case DRM_FORMAT_BGR888:
  4430. *depth = 24;
  4431. *bpp = 24;
  4432. break;
  4433. case DRM_FORMAT_XRGB8888:
  4434. case DRM_FORMAT_XBGR8888:
  4435. case DRM_FORMAT_RGBX8888:
  4436. case DRM_FORMAT_BGRX8888:
  4437. *depth = 24;
  4438. *bpp = 32;
  4439. break;
  4440. case DRM_FORMAT_XRGB2101010:
  4441. case DRM_FORMAT_XBGR2101010:
  4442. case DRM_FORMAT_RGBX1010102:
  4443. case DRM_FORMAT_BGRX1010102:
  4444. case DRM_FORMAT_ARGB2101010:
  4445. case DRM_FORMAT_ABGR2101010:
  4446. case DRM_FORMAT_RGBA1010102:
  4447. case DRM_FORMAT_BGRA1010102:
  4448. *depth = 30;
  4449. *bpp = 32;
  4450. break;
  4451. case DRM_FORMAT_ARGB8888:
  4452. case DRM_FORMAT_ABGR8888:
  4453. case DRM_FORMAT_RGBA8888:
  4454. case DRM_FORMAT_BGRA8888:
  4455. *depth = 32;
  4456. *bpp = 32;
  4457. break;
  4458. default:
  4459. DRM_DEBUG_KMS("unsupported pixel format %s\n",
  4460. drm_get_format_name(format));
  4461. *depth = 0;
  4462. *bpp = 0;
  4463. break;
  4464. }
  4465. }
  4466. EXPORT_SYMBOL(drm_fb_get_bpp_depth);
  4467. /**
  4468. * drm_format_num_planes - get the number of planes for format
  4469. * @format: pixel format (DRM_FORMAT_*)
  4470. *
  4471. * Returns:
  4472. * The number of planes used by the specified pixel format.
  4473. */
  4474. int drm_format_num_planes(uint32_t format)
  4475. {
  4476. switch (format) {
  4477. case DRM_FORMAT_YUV410:
  4478. case DRM_FORMAT_YVU410:
  4479. case DRM_FORMAT_YUV411:
  4480. case DRM_FORMAT_YVU411:
  4481. case DRM_FORMAT_YUV420:
  4482. case DRM_FORMAT_YVU420:
  4483. case DRM_FORMAT_YUV422:
  4484. case DRM_FORMAT_YVU422:
  4485. case DRM_FORMAT_YUV444:
  4486. case DRM_FORMAT_YVU444:
  4487. return 3;
  4488. case DRM_FORMAT_NV12:
  4489. case DRM_FORMAT_NV21:
  4490. case DRM_FORMAT_NV16:
  4491. case DRM_FORMAT_NV61:
  4492. case DRM_FORMAT_NV24:
  4493. case DRM_FORMAT_NV42:
  4494. return 2;
  4495. default:
  4496. return 1;
  4497. }
  4498. }
  4499. EXPORT_SYMBOL(drm_format_num_planes);
  4500. /**
  4501. * drm_format_plane_cpp - determine the bytes per pixel value
  4502. * @format: pixel format (DRM_FORMAT_*)
  4503. * @plane: plane index
  4504. *
  4505. * Returns:
  4506. * The bytes per pixel value for the specified plane.
  4507. */
  4508. int drm_format_plane_cpp(uint32_t format, int plane)
  4509. {
  4510. unsigned int depth;
  4511. int bpp;
  4512. if (plane >= drm_format_num_planes(format))
  4513. return 0;
  4514. switch (format) {
  4515. case DRM_FORMAT_YUYV:
  4516. case DRM_FORMAT_YVYU:
  4517. case DRM_FORMAT_UYVY:
  4518. case DRM_FORMAT_VYUY:
  4519. return 2;
  4520. case DRM_FORMAT_NV12:
  4521. case DRM_FORMAT_NV21:
  4522. case DRM_FORMAT_NV16:
  4523. case DRM_FORMAT_NV61:
  4524. case DRM_FORMAT_NV24:
  4525. case DRM_FORMAT_NV42:
  4526. return plane ? 2 : 1;
  4527. case DRM_FORMAT_YUV410:
  4528. case DRM_FORMAT_YVU410:
  4529. case DRM_FORMAT_YUV411:
  4530. case DRM_FORMAT_YVU411:
  4531. case DRM_FORMAT_YUV420:
  4532. case DRM_FORMAT_YVU420:
  4533. case DRM_FORMAT_YUV422:
  4534. case DRM_FORMAT_YVU422:
  4535. case DRM_FORMAT_YUV444:
  4536. case DRM_FORMAT_YVU444:
  4537. return 1;
  4538. default:
  4539. drm_fb_get_bpp_depth(format, &depth, &bpp);
  4540. return bpp >> 3;
  4541. }
  4542. }
  4543. EXPORT_SYMBOL(drm_format_plane_cpp);
  4544. /**
  4545. * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
  4546. * @format: pixel format (DRM_FORMAT_*)
  4547. *
  4548. * Returns:
  4549. * The horizontal chroma subsampling factor for the
  4550. * specified pixel format.
  4551. */
  4552. int drm_format_horz_chroma_subsampling(uint32_t format)
  4553. {
  4554. switch (format) {
  4555. case DRM_FORMAT_YUV411:
  4556. case DRM_FORMAT_YVU411:
  4557. case DRM_FORMAT_YUV410:
  4558. case DRM_FORMAT_YVU410:
  4559. return 4;
  4560. case DRM_FORMAT_YUYV:
  4561. case DRM_FORMAT_YVYU:
  4562. case DRM_FORMAT_UYVY:
  4563. case DRM_FORMAT_VYUY:
  4564. case DRM_FORMAT_NV12:
  4565. case DRM_FORMAT_NV21:
  4566. case DRM_FORMAT_NV16:
  4567. case DRM_FORMAT_NV61:
  4568. case DRM_FORMAT_YUV422:
  4569. case DRM_FORMAT_YVU422:
  4570. case DRM_FORMAT_YUV420:
  4571. case DRM_FORMAT_YVU420:
  4572. return 2;
  4573. default:
  4574. return 1;
  4575. }
  4576. }
  4577. EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
  4578. /**
  4579. * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
  4580. * @format: pixel format (DRM_FORMAT_*)
  4581. *
  4582. * Returns:
  4583. * The vertical chroma subsampling factor for the
  4584. * specified pixel format.
  4585. */
  4586. int drm_format_vert_chroma_subsampling(uint32_t format)
  4587. {
  4588. switch (format) {
  4589. case DRM_FORMAT_YUV410:
  4590. case DRM_FORMAT_YVU410:
  4591. return 4;
  4592. case DRM_FORMAT_YUV420:
  4593. case DRM_FORMAT_YVU420:
  4594. case DRM_FORMAT_NV12:
  4595. case DRM_FORMAT_NV21:
  4596. return 2;
  4597. default:
  4598. return 1;
  4599. }
  4600. }
  4601. EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
  4602. /**
  4603. * drm_rotation_simplify() - Try to simplify the rotation
  4604. * @rotation: Rotation to be simplified
  4605. * @supported_rotations: Supported rotations
  4606. *
  4607. * Attempt to simplify the rotation to a form that is supported.
  4608. * Eg. if the hardware supports everything except DRM_REFLECT_X
  4609. * one could call this function like this:
  4610. *
  4611. * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
  4612. * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
  4613. * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
  4614. *
  4615. * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
  4616. * transforms the hardware supports, this function may not
  4617. * be able to produce a supported transform, so the caller should
  4618. * check the result afterwards.
  4619. */
  4620. unsigned int drm_rotation_simplify(unsigned int rotation,
  4621. unsigned int supported_rotations)
  4622. {
  4623. if (rotation & ~supported_rotations) {
  4624. rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
  4625. rotation = (rotation & ~0xf) | BIT((ffs(rotation & 0xf) + 1) % 4);
  4626. }
  4627. return rotation;
  4628. }
  4629. EXPORT_SYMBOL(drm_rotation_simplify);
  4630. /**
  4631. * drm_mode_config_init - initialize DRM mode_configuration structure
  4632. * @dev: DRM device
  4633. *
  4634. * Initialize @dev's mode_config structure, used for tracking the graphics
  4635. * configuration of @dev.
  4636. *
  4637. * Since this initializes the modeset locks, no locking is possible. Which is no
  4638. * problem, since this should happen single threaded at init time. It is the
  4639. * driver's problem to ensure this guarantee.
  4640. *
  4641. */
  4642. void drm_mode_config_init(struct drm_device *dev)
  4643. {
  4644. mutex_init(&dev->mode_config.mutex);
  4645. drm_modeset_lock_init(&dev->mode_config.connection_mutex);
  4646. mutex_init(&dev->mode_config.idr_mutex);
  4647. mutex_init(&dev->mode_config.fb_lock);
  4648. INIT_LIST_HEAD(&dev->mode_config.fb_list);
  4649. INIT_LIST_HEAD(&dev->mode_config.crtc_list);
  4650. INIT_LIST_HEAD(&dev->mode_config.connector_list);
  4651. INIT_LIST_HEAD(&dev->mode_config.bridge_list);
  4652. INIT_LIST_HEAD(&dev->mode_config.encoder_list);
  4653. INIT_LIST_HEAD(&dev->mode_config.property_list);
  4654. INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
  4655. INIT_LIST_HEAD(&dev->mode_config.plane_list);
  4656. idr_init(&dev->mode_config.crtc_idr);
  4657. idr_init(&dev->mode_config.tile_idr);
  4658. drm_modeset_lock_all(dev);
  4659. drm_mode_create_standard_properties(dev);
  4660. drm_modeset_unlock_all(dev);
  4661. /* Just to be sure */
  4662. dev->mode_config.num_fb = 0;
  4663. dev->mode_config.num_connector = 0;
  4664. dev->mode_config.num_crtc = 0;
  4665. dev->mode_config.num_encoder = 0;
  4666. dev->mode_config.num_overlay_plane = 0;
  4667. dev->mode_config.num_total_plane = 0;
  4668. }
  4669. EXPORT_SYMBOL(drm_mode_config_init);
  4670. /**
  4671. * drm_mode_config_cleanup - free up DRM mode_config info
  4672. * @dev: DRM device
  4673. *
  4674. * Free up all the connectors and CRTCs associated with this DRM device, then
  4675. * free up the framebuffers and associated buffer objects.
  4676. *
  4677. * Note that since this /should/ happen single-threaded at driver/device
  4678. * teardown time, no locking is required. It's the driver's job to ensure that
  4679. * this guarantee actually holds true.
  4680. *
  4681. * FIXME: cleanup any dangling user buffer objects too
  4682. */
  4683. void drm_mode_config_cleanup(struct drm_device *dev)
  4684. {
  4685. struct drm_connector *connector, *ot;
  4686. struct drm_crtc *crtc, *ct;
  4687. struct drm_encoder *encoder, *enct;
  4688. struct drm_bridge *bridge, *brt;
  4689. struct drm_framebuffer *fb, *fbt;
  4690. struct drm_property *property, *pt;
  4691. struct drm_property_blob *blob, *bt;
  4692. struct drm_plane *plane, *plt;
  4693. list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
  4694. head) {
  4695. encoder->funcs->destroy(encoder);
  4696. }
  4697. list_for_each_entry_safe(bridge, brt,
  4698. &dev->mode_config.bridge_list, head) {
  4699. bridge->funcs->destroy(bridge);
  4700. }
  4701. list_for_each_entry_safe(connector, ot,
  4702. &dev->mode_config.connector_list, head) {
  4703. connector->funcs->destroy(connector);
  4704. }
  4705. list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
  4706. head) {
  4707. drm_property_destroy(dev, property);
  4708. }
  4709. list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
  4710. head) {
  4711. drm_property_destroy_blob(dev, blob);
  4712. }
  4713. /*
  4714. * Single-threaded teardown context, so it's not required to grab the
  4715. * fb_lock to protect against concurrent fb_list access. Contrary, it
  4716. * would actually deadlock with the drm_framebuffer_cleanup function.
  4717. *
  4718. * Also, if there are any framebuffers left, that's a driver leak now,
  4719. * so politely WARN about this.
  4720. */
  4721. WARN_ON(!list_empty(&dev->mode_config.fb_list));
  4722. list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
  4723. drm_framebuffer_remove(fb);
  4724. }
  4725. list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
  4726. head) {
  4727. plane->funcs->destroy(plane);
  4728. }
  4729. list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
  4730. crtc->funcs->destroy(crtc);
  4731. }
  4732. idr_destroy(&dev->mode_config.tile_idr);
  4733. idr_destroy(&dev->mode_config.crtc_idr);
  4734. drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
  4735. }
  4736. EXPORT_SYMBOL(drm_mode_config_cleanup);
  4737. struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
  4738. unsigned int supported_rotations)
  4739. {
  4740. static const struct drm_prop_enum_list props[] = {
  4741. { DRM_ROTATE_0, "rotate-0" },
  4742. { DRM_ROTATE_90, "rotate-90" },
  4743. { DRM_ROTATE_180, "rotate-180" },
  4744. { DRM_ROTATE_270, "rotate-270" },
  4745. { DRM_REFLECT_X, "reflect-x" },
  4746. { DRM_REFLECT_Y, "reflect-y" },
  4747. };
  4748. return drm_property_create_bitmask(dev, 0, "rotation",
  4749. props, ARRAY_SIZE(props),
  4750. supported_rotations);
  4751. }
  4752. EXPORT_SYMBOL(drm_mode_create_rotation_property);
  4753. /**
  4754. * DOC: Tile group
  4755. *
  4756. * Tile groups are used to represent tiled monitors with a unique
  4757. * integer identifier. Tiled monitors using DisplayID v1.3 have
  4758. * a unique 8-byte handle, we store this in a tile group, so we
  4759. * have a common identifier for all tiles in a monitor group.
  4760. */
  4761. static void drm_tile_group_free(struct kref *kref)
  4762. {
  4763. struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
  4764. struct drm_device *dev = tg->dev;
  4765. mutex_lock(&dev->mode_config.idr_mutex);
  4766. idr_remove(&dev->mode_config.tile_idr, tg->id);
  4767. mutex_unlock(&dev->mode_config.idr_mutex);
  4768. kfree(tg);
  4769. }
  4770. /**
  4771. * drm_mode_put_tile_group - drop a reference to a tile group.
  4772. * @dev: DRM device
  4773. * @tg: tile group to drop reference to.
  4774. *
  4775. * drop reference to tile group and free if 0.
  4776. */
  4777. void drm_mode_put_tile_group(struct drm_device *dev,
  4778. struct drm_tile_group *tg)
  4779. {
  4780. kref_put(&tg->refcount, drm_tile_group_free);
  4781. }
  4782. /**
  4783. * drm_mode_get_tile_group - get a reference to an existing tile group
  4784. * @dev: DRM device
  4785. * @topology: 8-bytes unique per monitor.
  4786. *
  4787. * Use the unique bytes to get a reference to an existing tile group.
  4788. *
  4789. * RETURNS:
  4790. * tile group or NULL if not found.
  4791. */
  4792. struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
  4793. char topology[8])
  4794. {
  4795. struct drm_tile_group *tg;
  4796. int id;
  4797. mutex_lock(&dev->mode_config.idr_mutex);
  4798. idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
  4799. if (!memcmp(tg->group_data, topology, 8)) {
  4800. if (!kref_get_unless_zero(&tg->refcount))
  4801. tg = NULL;
  4802. mutex_unlock(&dev->mode_config.idr_mutex);
  4803. return tg;
  4804. }
  4805. }
  4806. mutex_unlock(&dev->mode_config.idr_mutex);
  4807. return NULL;
  4808. }
  4809. /**
  4810. * drm_mode_create_tile_group - create a tile group from a displayid description
  4811. * @dev: DRM device
  4812. * @topology: 8-bytes unique per monitor.
  4813. *
  4814. * Create a tile group for the unique monitor, and get a unique
  4815. * identifier for the tile group.
  4816. *
  4817. * RETURNS:
  4818. * new tile group or error.
  4819. */
  4820. struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
  4821. char topology[8])
  4822. {
  4823. struct drm_tile_group *tg;
  4824. int ret;
  4825. tg = kzalloc(sizeof(*tg), GFP_KERNEL);
  4826. if (!tg)
  4827. return ERR_PTR(-ENOMEM);
  4828. kref_init(&tg->refcount);
  4829. memcpy(tg->group_data, topology, 8);
  4830. tg->dev = dev;
  4831. mutex_lock(&dev->mode_config.idr_mutex);
  4832. ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
  4833. if (ret >= 0) {
  4834. tg->id = ret;
  4835. } else {
  4836. kfree(tg);
  4837. tg = ERR_PTR(ret);
  4838. }
  4839. mutex_unlock(&dev->mode_config.idr_mutex);
  4840. return tg;
  4841. }