i915_gem.c 143 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560
  1. /*
  2. * Copyright © 2008-2015 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include <drm/drmP.h>
  28. #include <drm/drm_vma_manager.h>
  29. #include <drm/i915_drm.h>
  30. #include "i915_drv.h"
  31. #include "i915_vgpu.h"
  32. #include "i915_trace.h"
  33. #include "intel_drv.h"
  34. #include "intel_mocs.h"
  35. #include <linux/shmem_fs.h>
  36. #include <linux/slab.h>
  37. #include <linux/swap.h>
  38. #include <linux/pci.h>
  39. #include <linux/dma-buf.h>
  40. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  41. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
  42. static void
  43. i915_gem_object_retire__write(struct drm_i915_gem_object *obj);
  44. static void
  45. i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int ring);
  46. static bool cpu_cache_is_coherent(struct drm_device *dev,
  47. enum i915_cache_level level)
  48. {
  49. return HAS_LLC(dev) || level != I915_CACHE_NONE;
  50. }
  51. static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  52. {
  53. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  54. return false;
  55. if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
  56. return true;
  57. return obj->pin_display;
  58. }
  59. static int
  60. insert_mappable_node(struct drm_i915_private *i915,
  61. struct drm_mm_node *node, u32 size)
  62. {
  63. memset(node, 0, sizeof(*node));
  64. return drm_mm_insert_node_in_range_generic(&i915->ggtt.base.mm, node,
  65. size, 0, 0, 0,
  66. i915->ggtt.mappable_end,
  67. DRM_MM_SEARCH_DEFAULT,
  68. DRM_MM_CREATE_DEFAULT);
  69. }
  70. static void
  71. remove_mappable_node(struct drm_mm_node *node)
  72. {
  73. drm_mm_remove_node(node);
  74. }
  75. /* some bookkeeping */
  76. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  77. size_t size)
  78. {
  79. spin_lock(&dev_priv->mm.object_stat_lock);
  80. dev_priv->mm.object_count++;
  81. dev_priv->mm.object_memory += size;
  82. spin_unlock(&dev_priv->mm.object_stat_lock);
  83. }
  84. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  85. size_t size)
  86. {
  87. spin_lock(&dev_priv->mm.object_stat_lock);
  88. dev_priv->mm.object_count--;
  89. dev_priv->mm.object_memory -= size;
  90. spin_unlock(&dev_priv->mm.object_stat_lock);
  91. }
  92. static int
  93. i915_gem_wait_for_error(struct i915_gpu_error *error)
  94. {
  95. int ret;
  96. if (!i915_reset_in_progress(error))
  97. return 0;
  98. /*
  99. * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  100. * userspace. If it takes that long something really bad is going on and
  101. * we should simply try to bail out and fail as gracefully as possible.
  102. */
  103. ret = wait_event_interruptible_timeout(error->reset_queue,
  104. !i915_reset_in_progress(error),
  105. 10*HZ);
  106. if (ret == 0) {
  107. DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  108. return -EIO;
  109. } else if (ret < 0) {
  110. return ret;
  111. } else {
  112. return 0;
  113. }
  114. }
  115. int i915_mutex_lock_interruptible(struct drm_device *dev)
  116. {
  117. struct drm_i915_private *dev_priv = dev->dev_private;
  118. int ret;
  119. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  120. if (ret)
  121. return ret;
  122. ret = mutex_lock_interruptible(&dev->struct_mutex);
  123. if (ret)
  124. return ret;
  125. WARN_ON(i915_verify_lists(dev));
  126. return 0;
  127. }
  128. int
  129. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  130. struct drm_file *file)
  131. {
  132. struct drm_i915_private *dev_priv = to_i915(dev);
  133. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  134. struct drm_i915_gem_get_aperture *args = data;
  135. struct i915_vma *vma;
  136. size_t pinned;
  137. pinned = 0;
  138. mutex_lock(&dev->struct_mutex);
  139. list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
  140. if (vma->pin_count)
  141. pinned += vma->node.size;
  142. list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
  143. if (vma->pin_count)
  144. pinned += vma->node.size;
  145. mutex_unlock(&dev->struct_mutex);
  146. args->aper_size = ggtt->base.total;
  147. args->aper_available_size = args->aper_size - pinned;
  148. return 0;
  149. }
  150. static int
  151. i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
  152. {
  153. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  154. char *vaddr = obj->phys_handle->vaddr;
  155. struct sg_table *st;
  156. struct scatterlist *sg;
  157. int i;
  158. if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
  159. return -EINVAL;
  160. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  161. struct page *page;
  162. char *src;
  163. page = shmem_read_mapping_page(mapping, i);
  164. if (IS_ERR(page))
  165. return PTR_ERR(page);
  166. src = kmap_atomic(page);
  167. memcpy(vaddr, src, PAGE_SIZE);
  168. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  169. kunmap_atomic(src);
  170. put_page(page);
  171. vaddr += PAGE_SIZE;
  172. }
  173. i915_gem_chipset_flush(to_i915(obj->base.dev));
  174. st = kmalloc(sizeof(*st), GFP_KERNEL);
  175. if (st == NULL)
  176. return -ENOMEM;
  177. if (sg_alloc_table(st, 1, GFP_KERNEL)) {
  178. kfree(st);
  179. return -ENOMEM;
  180. }
  181. sg = st->sgl;
  182. sg->offset = 0;
  183. sg->length = obj->base.size;
  184. sg_dma_address(sg) = obj->phys_handle->busaddr;
  185. sg_dma_len(sg) = obj->base.size;
  186. obj->pages = st;
  187. return 0;
  188. }
  189. static void
  190. i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj)
  191. {
  192. int ret;
  193. BUG_ON(obj->madv == __I915_MADV_PURGED);
  194. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  195. if (WARN_ON(ret)) {
  196. /* In the event of a disaster, abandon all caches and
  197. * hope for the best.
  198. */
  199. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  200. }
  201. if (obj->madv == I915_MADV_DONTNEED)
  202. obj->dirty = 0;
  203. if (obj->dirty) {
  204. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  205. char *vaddr = obj->phys_handle->vaddr;
  206. int i;
  207. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  208. struct page *page;
  209. char *dst;
  210. page = shmem_read_mapping_page(mapping, i);
  211. if (IS_ERR(page))
  212. continue;
  213. dst = kmap_atomic(page);
  214. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  215. memcpy(dst, vaddr, PAGE_SIZE);
  216. kunmap_atomic(dst);
  217. set_page_dirty(page);
  218. if (obj->madv == I915_MADV_WILLNEED)
  219. mark_page_accessed(page);
  220. put_page(page);
  221. vaddr += PAGE_SIZE;
  222. }
  223. obj->dirty = 0;
  224. }
  225. sg_free_table(obj->pages);
  226. kfree(obj->pages);
  227. }
  228. static void
  229. i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
  230. {
  231. drm_pci_free(obj->base.dev, obj->phys_handle);
  232. }
  233. static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
  234. .get_pages = i915_gem_object_get_pages_phys,
  235. .put_pages = i915_gem_object_put_pages_phys,
  236. .release = i915_gem_object_release_phys,
  237. };
  238. static int
  239. drop_pages(struct drm_i915_gem_object *obj)
  240. {
  241. struct i915_vma *vma, *next;
  242. int ret;
  243. drm_gem_object_reference(&obj->base);
  244. list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link)
  245. if (i915_vma_unbind(vma))
  246. break;
  247. ret = i915_gem_object_put_pages(obj);
  248. drm_gem_object_unreference(&obj->base);
  249. return ret;
  250. }
  251. int
  252. i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
  253. int align)
  254. {
  255. drm_dma_handle_t *phys;
  256. int ret;
  257. if (obj->phys_handle) {
  258. if ((unsigned long)obj->phys_handle->vaddr & (align -1))
  259. return -EBUSY;
  260. return 0;
  261. }
  262. if (obj->madv != I915_MADV_WILLNEED)
  263. return -EFAULT;
  264. if (obj->base.filp == NULL)
  265. return -EINVAL;
  266. ret = drop_pages(obj);
  267. if (ret)
  268. return ret;
  269. /* create a new object */
  270. phys = drm_pci_alloc(obj->base.dev, obj->base.size, align);
  271. if (!phys)
  272. return -ENOMEM;
  273. obj->phys_handle = phys;
  274. obj->ops = &i915_gem_phys_ops;
  275. return i915_gem_object_get_pages(obj);
  276. }
  277. static int
  278. i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
  279. struct drm_i915_gem_pwrite *args,
  280. struct drm_file *file_priv)
  281. {
  282. struct drm_device *dev = obj->base.dev;
  283. void *vaddr = obj->phys_handle->vaddr + args->offset;
  284. char __user *user_data = u64_to_user_ptr(args->data_ptr);
  285. int ret = 0;
  286. /* We manually control the domain here and pretend that it
  287. * remains coherent i.e. in the GTT domain, like shmem_pwrite.
  288. */
  289. ret = i915_gem_object_wait_rendering(obj, false);
  290. if (ret)
  291. return ret;
  292. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  293. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  294. unsigned long unwritten;
  295. /* The physical object once assigned is fixed for the lifetime
  296. * of the obj, so we can safely drop the lock and continue
  297. * to access vaddr.
  298. */
  299. mutex_unlock(&dev->struct_mutex);
  300. unwritten = copy_from_user(vaddr, user_data, args->size);
  301. mutex_lock(&dev->struct_mutex);
  302. if (unwritten) {
  303. ret = -EFAULT;
  304. goto out;
  305. }
  306. }
  307. drm_clflush_virt_range(vaddr, args->size);
  308. i915_gem_chipset_flush(to_i915(dev));
  309. out:
  310. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  311. return ret;
  312. }
  313. void *i915_gem_object_alloc(struct drm_device *dev)
  314. {
  315. struct drm_i915_private *dev_priv = dev->dev_private;
  316. return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
  317. }
  318. void i915_gem_object_free(struct drm_i915_gem_object *obj)
  319. {
  320. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  321. kmem_cache_free(dev_priv->objects, obj);
  322. }
  323. static int
  324. i915_gem_create(struct drm_file *file,
  325. struct drm_device *dev,
  326. uint64_t size,
  327. uint32_t *handle_p)
  328. {
  329. struct drm_i915_gem_object *obj;
  330. int ret;
  331. u32 handle;
  332. size = roundup(size, PAGE_SIZE);
  333. if (size == 0)
  334. return -EINVAL;
  335. /* Allocate the new object */
  336. obj = i915_gem_object_create(dev, size);
  337. if (IS_ERR(obj))
  338. return PTR_ERR(obj);
  339. ret = drm_gem_handle_create(file, &obj->base, &handle);
  340. /* drop reference from allocate - handle holds it now */
  341. drm_gem_object_unreference_unlocked(&obj->base);
  342. if (ret)
  343. return ret;
  344. *handle_p = handle;
  345. return 0;
  346. }
  347. int
  348. i915_gem_dumb_create(struct drm_file *file,
  349. struct drm_device *dev,
  350. struct drm_mode_create_dumb *args)
  351. {
  352. /* have to work out size/pitch and return them */
  353. args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
  354. args->size = args->pitch * args->height;
  355. return i915_gem_create(file, dev,
  356. args->size, &args->handle);
  357. }
  358. /**
  359. * Creates a new mm object and returns a handle to it.
  360. * @dev: drm device pointer
  361. * @data: ioctl data blob
  362. * @file: drm file pointer
  363. */
  364. int
  365. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  366. struct drm_file *file)
  367. {
  368. struct drm_i915_gem_create *args = data;
  369. return i915_gem_create(file, dev,
  370. args->size, &args->handle);
  371. }
  372. static inline int
  373. __copy_to_user_swizzled(char __user *cpu_vaddr,
  374. const char *gpu_vaddr, int gpu_offset,
  375. int length)
  376. {
  377. int ret, cpu_offset = 0;
  378. while (length > 0) {
  379. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  380. int this_length = min(cacheline_end - gpu_offset, length);
  381. int swizzled_gpu_offset = gpu_offset ^ 64;
  382. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  383. gpu_vaddr + swizzled_gpu_offset,
  384. this_length);
  385. if (ret)
  386. return ret + length;
  387. cpu_offset += this_length;
  388. gpu_offset += this_length;
  389. length -= this_length;
  390. }
  391. return 0;
  392. }
  393. static inline int
  394. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  395. const char __user *cpu_vaddr,
  396. int length)
  397. {
  398. int ret, cpu_offset = 0;
  399. while (length > 0) {
  400. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  401. int this_length = min(cacheline_end - gpu_offset, length);
  402. int swizzled_gpu_offset = gpu_offset ^ 64;
  403. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  404. cpu_vaddr + cpu_offset,
  405. this_length);
  406. if (ret)
  407. return ret + length;
  408. cpu_offset += this_length;
  409. gpu_offset += this_length;
  410. length -= this_length;
  411. }
  412. return 0;
  413. }
  414. /*
  415. * Pins the specified object's pages and synchronizes the object with
  416. * GPU accesses. Sets needs_clflush to non-zero if the caller should
  417. * flush the object from the CPU cache.
  418. */
  419. int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
  420. int *needs_clflush)
  421. {
  422. int ret;
  423. *needs_clflush = 0;
  424. if (WARN_ON(!i915_gem_object_has_struct_page(obj)))
  425. return -EINVAL;
  426. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
  427. /* If we're not in the cpu read domain, set ourself into the gtt
  428. * read domain and manually flush cachelines (if required). This
  429. * optimizes for the case when the gpu will dirty the data
  430. * anyway again before the next pread happens. */
  431. *needs_clflush = !cpu_cache_is_coherent(obj->base.dev,
  432. obj->cache_level);
  433. ret = i915_gem_object_wait_rendering(obj, true);
  434. if (ret)
  435. return ret;
  436. }
  437. ret = i915_gem_object_get_pages(obj);
  438. if (ret)
  439. return ret;
  440. i915_gem_object_pin_pages(obj);
  441. return ret;
  442. }
  443. /* Per-page copy function for the shmem pread fastpath.
  444. * Flushes invalid cachelines before reading the target if
  445. * needs_clflush is set. */
  446. static int
  447. shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
  448. char __user *user_data,
  449. bool page_do_bit17_swizzling, bool needs_clflush)
  450. {
  451. char *vaddr;
  452. int ret;
  453. if (unlikely(page_do_bit17_swizzling))
  454. return -EINVAL;
  455. vaddr = kmap_atomic(page);
  456. if (needs_clflush)
  457. drm_clflush_virt_range(vaddr + shmem_page_offset,
  458. page_length);
  459. ret = __copy_to_user_inatomic(user_data,
  460. vaddr + shmem_page_offset,
  461. page_length);
  462. kunmap_atomic(vaddr);
  463. return ret ? -EFAULT : 0;
  464. }
  465. static void
  466. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  467. bool swizzled)
  468. {
  469. if (unlikely(swizzled)) {
  470. unsigned long start = (unsigned long) addr;
  471. unsigned long end = (unsigned long) addr + length;
  472. /* For swizzling simply ensure that we always flush both
  473. * channels. Lame, but simple and it works. Swizzled
  474. * pwrite/pread is far from a hotpath - current userspace
  475. * doesn't use it at all. */
  476. start = round_down(start, 128);
  477. end = round_up(end, 128);
  478. drm_clflush_virt_range((void *)start, end - start);
  479. } else {
  480. drm_clflush_virt_range(addr, length);
  481. }
  482. }
  483. /* Only difference to the fast-path function is that this can handle bit17
  484. * and uses non-atomic copy and kmap functions. */
  485. static int
  486. shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
  487. char __user *user_data,
  488. bool page_do_bit17_swizzling, bool needs_clflush)
  489. {
  490. char *vaddr;
  491. int ret;
  492. vaddr = kmap(page);
  493. if (needs_clflush)
  494. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  495. page_length,
  496. page_do_bit17_swizzling);
  497. if (page_do_bit17_swizzling)
  498. ret = __copy_to_user_swizzled(user_data,
  499. vaddr, shmem_page_offset,
  500. page_length);
  501. else
  502. ret = __copy_to_user(user_data,
  503. vaddr + shmem_page_offset,
  504. page_length);
  505. kunmap(page);
  506. return ret ? - EFAULT : 0;
  507. }
  508. static inline unsigned long
  509. slow_user_access(struct io_mapping *mapping,
  510. uint64_t page_base, int page_offset,
  511. char __user *user_data,
  512. unsigned long length, bool pwrite)
  513. {
  514. void __iomem *ioaddr;
  515. void *vaddr;
  516. uint64_t unwritten;
  517. ioaddr = io_mapping_map_wc(mapping, page_base, PAGE_SIZE);
  518. /* We can use the cpu mem copy function because this is X86. */
  519. vaddr = (void __force *)ioaddr + page_offset;
  520. if (pwrite)
  521. unwritten = __copy_from_user(vaddr, user_data, length);
  522. else
  523. unwritten = __copy_to_user(user_data, vaddr, length);
  524. io_mapping_unmap(ioaddr);
  525. return unwritten;
  526. }
  527. static int
  528. i915_gem_gtt_pread(struct drm_device *dev,
  529. struct drm_i915_gem_object *obj, uint64_t size,
  530. uint64_t data_offset, uint64_t data_ptr)
  531. {
  532. struct drm_i915_private *dev_priv = dev->dev_private;
  533. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  534. struct drm_mm_node node;
  535. char __user *user_data;
  536. uint64_t remain;
  537. uint64_t offset;
  538. int ret;
  539. ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE);
  540. if (ret) {
  541. ret = insert_mappable_node(dev_priv, &node, PAGE_SIZE);
  542. if (ret)
  543. goto out;
  544. ret = i915_gem_object_get_pages(obj);
  545. if (ret) {
  546. remove_mappable_node(&node);
  547. goto out;
  548. }
  549. i915_gem_object_pin_pages(obj);
  550. } else {
  551. node.start = i915_gem_obj_ggtt_offset(obj);
  552. node.allocated = false;
  553. ret = i915_gem_object_put_fence(obj);
  554. if (ret)
  555. goto out_unpin;
  556. }
  557. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  558. if (ret)
  559. goto out_unpin;
  560. user_data = u64_to_user_ptr(data_ptr);
  561. remain = size;
  562. offset = data_offset;
  563. mutex_unlock(&dev->struct_mutex);
  564. if (likely(!i915.prefault_disable)) {
  565. ret = fault_in_multipages_writeable(user_data, remain);
  566. if (ret) {
  567. mutex_lock(&dev->struct_mutex);
  568. goto out_unpin;
  569. }
  570. }
  571. while (remain > 0) {
  572. /* Operation in this page
  573. *
  574. * page_base = page offset within aperture
  575. * page_offset = offset within page
  576. * page_length = bytes to copy for this page
  577. */
  578. u32 page_base = node.start;
  579. unsigned page_offset = offset_in_page(offset);
  580. unsigned page_length = PAGE_SIZE - page_offset;
  581. page_length = remain < page_length ? remain : page_length;
  582. if (node.allocated) {
  583. wmb();
  584. ggtt->base.insert_page(&ggtt->base,
  585. i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
  586. node.start,
  587. I915_CACHE_NONE, 0);
  588. wmb();
  589. } else {
  590. page_base += offset & PAGE_MASK;
  591. }
  592. /* This is a slow read/write as it tries to read from
  593. * and write to user memory which may result into page
  594. * faults, and so we cannot perform this under struct_mutex.
  595. */
  596. if (slow_user_access(ggtt->mappable, page_base,
  597. page_offset, user_data,
  598. page_length, false)) {
  599. ret = -EFAULT;
  600. break;
  601. }
  602. remain -= page_length;
  603. user_data += page_length;
  604. offset += page_length;
  605. }
  606. mutex_lock(&dev->struct_mutex);
  607. if (ret == 0 && (obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) {
  608. /* The user has modified the object whilst we tried
  609. * reading from it, and we now have no idea what domain
  610. * the pages should be in. As we have just been touching
  611. * them directly, flush everything back to the GTT
  612. * domain.
  613. */
  614. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  615. }
  616. out_unpin:
  617. if (node.allocated) {
  618. wmb();
  619. ggtt->base.clear_range(&ggtt->base,
  620. node.start, node.size,
  621. true);
  622. i915_gem_object_unpin_pages(obj);
  623. remove_mappable_node(&node);
  624. } else {
  625. i915_gem_object_ggtt_unpin(obj);
  626. }
  627. out:
  628. return ret;
  629. }
  630. static int
  631. i915_gem_shmem_pread(struct drm_device *dev,
  632. struct drm_i915_gem_object *obj,
  633. struct drm_i915_gem_pread *args,
  634. struct drm_file *file)
  635. {
  636. char __user *user_data;
  637. ssize_t remain;
  638. loff_t offset;
  639. int shmem_page_offset, page_length, ret = 0;
  640. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  641. int prefaulted = 0;
  642. int needs_clflush = 0;
  643. struct sg_page_iter sg_iter;
  644. if (!i915_gem_object_has_struct_page(obj))
  645. return -ENODEV;
  646. user_data = u64_to_user_ptr(args->data_ptr);
  647. remain = args->size;
  648. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  649. ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
  650. if (ret)
  651. return ret;
  652. offset = args->offset;
  653. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  654. offset >> PAGE_SHIFT) {
  655. struct page *page = sg_page_iter_page(&sg_iter);
  656. if (remain <= 0)
  657. break;
  658. /* Operation in this page
  659. *
  660. * shmem_page_offset = offset within page in shmem file
  661. * page_length = bytes to copy for this page
  662. */
  663. shmem_page_offset = offset_in_page(offset);
  664. page_length = remain;
  665. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  666. page_length = PAGE_SIZE - shmem_page_offset;
  667. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  668. (page_to_phys(page) & (1 << 17)) != 0;
  669. ret = shmem_pread_fast(page, shmem_page_offset, page_length,
  670. user_data, page_do_bit17_swizzling,
  671. needs_clflush);
  672. if (ret == 0)
  673. goto next_page;
  674. mutex_unlock(&dev->struct_mutex);
  675. if (likely(!i915.prefault_disable) && !prefaulted) {
  676. ret = fault_in_multipages_writeable(user_data, remain);
  677. /* Userspace is tricking us, but we've already clobbered
  678. * its pages with the prefault and promised to write the
  679. * data up to the first fault. Hence ignore any errors
  680. * and just continue. */
  681. (void)ret;
  682. prefaulted = 1;
  683. }
  684. ret = shmem_pread_slow(page, shmem_page_offset, page_length,
  685. user_data, page_do_bit17_swizzling,
  686. needs_clflush);
  687. mutex_lock(&dev->struct_mutex);
  688. if (ret)
  689. goto out;
  690. next_page:
  691. remain -= page_length;
  692. user_data += page_length;
  693. offset += page_length;
  694. }
  695. out:
  696. i915_gem_object_unpin_pages(obj);
  697. return ret;
  698. }
  699. /**
  700. * Reads data from the object referenced by handle.
  701. * @dev: drm device pointer
  702. * @data: ioctl data blob
  703. * @file: drm file pointer
  704. *
  705. * On error, the contents of *data are undefined.
  706. */
  707. int
  708. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  709. struct drm_file *file)
  710. {
  711. struct drm_i915_gem_pread *args = data;
  712. struct drm_i915_gem_object *obj;
  713. int ret = 0;
  714. if (args->size == 0)
  715. return 0;
  716. if (!access_ok(VERIFY_WRITE,
  717. u64_to_user_ptr(args->data_ptr),
  718. args->size))
  719. return -EFAULT;
  720. ret = i915_mutex_lock_interruptible(dev);
  721. if (ret)
  722. return ret;
  723. obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
  724. if (&obj->base == NULL) {
  725. ret = -ENOENT;
  726. goto unlock;
  727. }
  728. /* Bounds check source. */
  729. if (args->offset > obj->base.size ||
  730. args->size > obj->base.size - args->offset) {
  731. ret = -EINVAL;
  732. goto out;
  733. }
  734. trace_i915_gem_object_pread(obj, args->offset, args->size);
  735. ret = i915_gem_shmem_pread(dev, obj, args, file);
  736. /* pread for non shmem backed objects */
  737. if (ret == -EFAULT || ret == -ENODEV)
  738. ret = i915_gem_gtt_pread(dev, obj, args->size,
  739. args->offset, args->data_ptr);
  740. out:
  741. drm_gem_object_unreference(&obj->base);
  742. unlock:
  743. mutex_unlock(&dev->struct_mutex);
  744. return ret;
  745. }
  746. /* This is the fast write path which cannot handle
  747. * page faults in the source data
  748. */
  749. static inline int
  750. fast_user_write(struct io_mapping *mapping,
  751. loff_t page_base, int page_offset,
  752. char __user *user_data,
  753. int length)
  754. {
  755. void __iomem *vaddr_atomic;
  756. void *vaddr;
  757. unsigned long unwritten;
  758. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  759. /* We can use the cpu mem copy function because this is X86. */
  760. vaddr = (void __force*)vaddr_atomic + page_offset;
  761. unwritten = __copy_from_user_inatomic_nocache(vaddr,
  762. user_data, length);
  763. io_mapping_unmap_atomic(vaddr_atomic);
  764. return unwritten;
  765. }
  766. /**
  767. * This is the fast pwrite path, where we copy the data directly from the
  768. * user into the GTT, uncached.
  769. * @dev: drm device pointer
  770. * @obj: i915 gem object
  771. * @args: pwrite arguments structure
  772. * @file: drm file pointer
  773. */
  774. static int
  775. i915_gem_gtt_pwrite_fast(struct drm_i915_private *i915,
  776. struct drm_i915_gem_object *obj,
  777. struct drm_i915_gem_pwrite *args,
  778. struct drm_file *file)
  779. {
  780. struct i915_ggtt *ggtt = &i915->ggtt;
  781. struct drm_device *dev = obj->base.dev;
  782. struct drm_mm_node node;
  783. uint64_t remain, offset;
  784. char __user *user_data;
  785. int ret;
  786. bool hit_slow_path = false;
  787. if (obj->tiling_mode != I915_TILING_NONE)
  788. return -EFAULT;
  789. ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE | PIN_NONBLOCK);
  790. if (ret) {
  791. ret = insert_mappable_node(i915, &node, PAGE_SIZE);
  792. if (ret)
  793. goto out;
  794. ret = i915_gem_object_get_pages(obj);
  795. if (ret) {
  796. remove_mappable_node(&node);
  797. goto out;
  798. }
  799. i915_gem_object_pin_pages(obj);
  800. } else {
  801. node.start = i915_gem_obj_ggtt_offset(obj);
  802. node.allocated = false;
  803. ret = i915_gem_object_put_fence(obj);
  804. if (ret)
  805. goto out_unpin;
  806. }
  807. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  808. if (ret)
  809. goto out_unpin;
  810. intel_fb_obj_invalidate(obj, ORIGIN_GTT);
  811. obj->dirty = true;
  812. user_data = u64_to_user_ptr(args->data_ptr);
  813. offset = args->offset;
  814. remain = args->size;
  815. while (remain) {
  816. /* Operation in this page
  817. *
  818. * page_base = page offset within aperture
  819. * page_offset = offset within page
  820. * page_length = bytes to copy for this page
  821. */
  822. u32 page_base = node.start;
  823. unsigned page_offset = offset_in_page(offset);
  824. unsigned page_length = PAGE_SIZE - page_offset;
  825. page_length = remain < page_length ? remain : page_length;
  826. if (node.allocated) {
  827. wmb(); /* flush the write before we modify the GGTT */
  828. ggtt->base.insert_page(&ggtt->base,
  829. i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
  830. node.start, I915_CACHE_NONE, 0);
  831. wmb(); /* flush modifications to the GGTT (insert_page) */
  832. } else {
  833. page_base += offset & PAGE_MASK;
  834. }
  835. /* If we get a fault while copying data, then (presumably) our
  836. * source page isn't available. Return the error and we'll
  837. * retry in the slow path.
  838. * If the object is non-shmem backed, we retry again with the
  839. * path that handles page fault.
  840. */
  841. if (fast_user_write(ggtt->mappable, page_base,
  842. page_offset, user_data, page_length)) {
  843. hit_slow_path = true;
  844. mutex_unlock(&dev->struct_mutex);
  845. if (slow_user_access(ggtt->mappable,
  846. page_base,
  847. page_offset, user_data,
  848. page_length, true)) {
  849. ret = -EFAULT;
  850. mutex_lock(&dev->struct_mutex);
  851. goto out_flush;
  852. }
  853. mutex_lock(&dev->struct_mutex);
  854. }
  855. remain -= page_length;
  856. user_data += page_length;
  857. offset += page_length;
  858. }
  859. out_flush:
  860. if (hit_slow_path) {
  861. if (ret == 0 &&
  862. (obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) {
  863. /* The user has modified the object whilst we tried
  864. * reading from it, and we now have no idea what domain
  865. * the pages should be in. As we have just been touching
  866. * them directly, flush everything back to the GTT
  867. * domain.
  868. */
  869. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  870. }
  871. }
  872. intel_fb_obj_flush(obj, false, ORIGIN_GTT);
  873. out_unpin:
  874. if (node.allocated) {
  875. wmb();
  876. ggtt->base.clear_range(&ggtt->base,
  877. node.start, node.size,
  878. true);
  879. i915_gem_object_unpin_pages(obj);
  880. remove_mappable_node(&node);
  881. } else {
  882. i915_gem_object_ggtt_unpin(obj);
  883. }
  884. out:
  885. return ret;
  886. }
  887. /* Per-page copy function for the shmem pwrite fastpath.
  888. * Flushes invalid cachelines before writing to the target if
  889. * needs_clflush_before is set and flushes out any written cachelines after
  890. * writing if needs_clflush is set. */
  891. static int
  892. shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
  893. char __user *user_data,
  894. bool page_do_bit17_swizzling,
  895. bool needs_clflush_before,
  896. bool needs_clflush_after)
  897. {
  898. char *vaddr;
  899. int ret;
  900. if (unlikely(page_do_bit17_swizzling))
  901. return -EINVAL;
  902. vaddr = kmap_atomic(page);
  903. if (needs_clflush_before)
  904. drm_clflush_virt_range(vaddr + shmem_page_offset,
  905. page_length);
  906. ret = __copy_from_user_inatomic(vaddr + shmem_page_offset,
  907. user_data, page_length);
  908. if (needs_clflush_after)
  909. drm_clflush_virt_range(vaddr + shmem_page_offset,
  910. page_length);
  911. kunmap_atomic(vaddr);
  912. return ret ? -EFAULT : 0;
  913. }
  914. /* Only difference to the fast-path function is that this can handle bit17
  915. * and uses non-atomic copy and kmap functions. */
  916. static int
  917. shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
  918. char __user *user_data,
  919. bool page_do_bit17_swizzling,
  920. bool needs_clflush_before,
  921. bool needs_clflush_after)
  922. {
  923. char *vaddr;
  924. int ret;
  925. vaddr = kmap(page);
  926. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  927. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  928. page_length,
  929. page_do_bit17_swizzling);
  930. if (page_do_bit17_swizzling)
  931. ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
  932. user_data,
  933. page_length);
  934. else
  935. ret = __copy_from_user(vaddr + shmem_page_offset,
  936. user_data,
  937. page_length);
  938. if (needs_clflush_after)
  939. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  940. page_length,
  941. page_do_bit17_swizzling);
  942. kunmap(page);
  943. return ret ? -EFAULT : 0;
  944. }
  945. static int
  946. i915_gem_shmem_pwrite(struct drm_device *dev,
  947. struct drm_i915_gem_object *obj,
  948. struct drm_i915_gem_pwrite *args,
  949. struct drm_file *file)
  950. {
  951. ssize_t remain;
  952. loff_t offset;
  953. char __user *user_data;
  954. int shmem_page_offset, page_length, ret = 0;
  955. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  956. int hit_slowpath = 0;
  957. int needs_clflush_after = 0;
  958. int needs_clflush_before = 0;
  959. struct sg_page_iter sg_iter;
  960. user_data = u64_to_user_ptr(args->data_ptr);
  961. remain = args->size;
  962. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  963. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  964. /* If we're not in the cpu write domain, set ourself into the gtt
  965. * write domain and manually flush cachelines (if required). This
  966. * optimizes for the case when the gpu will use the data
  967. * right away and we therefore have to clflush anyway. */
  968. needs_clflush_after = cpu_write_needs_clflush(obj);
  969. ret = i915_gem_object_wait_rendering(obj, false);
  970. if (ret)
  971. return ret;
  972. }
  973. /* Same trick applies to invalidate partially written cachelines read
  974. * before writing. */
  975. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
  976. needs_clflush_before =
  977. !cpu_cache_is_coherent(dev, obj->cache_level);
  978. ret = i915_gem_object_get_pages(obj);
  979. if (ret)
  980. return ret;
  981. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  982. i915_gem_object_pin_pages(obj);
  983. offset = args->offset;
  984. obj->dirty = 1;
  985. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  986. offset >> PAGE_SHIFT) {
  987. struct page *page = sg_page_iter_page(&sg_iter);
  988. int partial_cacheline_write;
  989. if (remain <= 0)
  990. break;
  991. /* Operation in this page
  992. *
  993. * shmem_page_offset = offset within page in shmem file
  994. * page_length = bytes to copy for this page
  995. */
  996. shmem_page_offset = offset_in_page(offset);
  997. page_length = remain;
  998. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  999. page_length = PAGE_SIZE - shmem_page_offset;
  1000. /* If we don't overwrite a cacheline completely we need to be
  1001. * careful to have up-to-date data by first clflushing. Don't
  1002. * overcomplicate things and flush the entire patch. */
  1003. partial_cacheline_write = needs_clflush_before &&
  1004. ((shmem_page_offset | page_length)
  1005. & (boot_cpu_data.x86_clflush_size - 1));
  1006. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  1007. (page_to_phys(page) & (1 << 17)) != 0;
  1008. ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
  1009. user_data, page_do_bit17_swizzling,
  1010. partial_cacheline_write,
  1011. needs_clflush_after);
  1012. if (ret == 0)
  1013. goto next_page;
  1014. hit_slowpath = 1;
  1015. mutex_unlock(&dev->struct_mutex);
  1016. ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
  1017. user_data, page_do_bit17_swizzling,
  1018. partial_cacheline_write,
  1019. needs_clflush_after);
  1020. mutex_lock(&dev->struct_mutex);
  1021. if (ret)
  1022. goto out;
  1023. next_page:
  1024. remain -= page_length;
  1025. user_data += page_length;
  1026. offset += page_length;
  1027. }
  1028. out:
  1029. i915_gem_object_unpin_pages(obj);
  1030. if (hit_slowpath) {
  1031. /*
  1032. * Fixup: Flush cpu caches in case we didn't flush the dirty
  1033. * cachelines in-line while writing and the object moved
  1034. * out of the cpu write domain while we've dropped the lock.
  1035. */
  1036. if (!needs_clflush_after &&
  1037. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  1038. if (i915_gem_clflush_object(obj, obj->pin_display))
  1039. needs_clflush_after = true;
  1040. }
  1041. }
  1042. if (needs_clflush_after)
  1043. i915_gem_chipset_flush(to_i915(dev));
  1044. else
  1045. obj->cache_dirty = true;
  1046. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  1047. return ret;
  1048. }
  1049. /**
  1050. * Writes data to the object referenced by handle.
  1051. * @dev: drm device
  1052. * @data: ioctl data blob
  1053. * @file: drm file
  1054. *
  1055. * On error, the contents of the buffer that were to be modified are undefined.
  1056. */
  1057. int
  1058. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  1059. struct drm_file *file)
  1060. {
  1061. struct drm_i915_private *dev_priv = dev->dev_private;
  1062. struct drm_i915_gem_pwrite *args = data;
  1063. struct drm_i915_gem_object *obj;
  1064. int ret;
  1065. if (args->size == 0)
  1066. return 0;
  1067. if (!access_ok(VERIFY_READ,
  1068. u64_to_user_ptr(args->data_ptr),
  1069. args->size))
  1070. return -EFAULT;
  1071. if (likely(!i915.prefault_disable)) {
  1072. ret = fault_in_multipages_readable(u64_to_user_ptr(args->data_ptr),
  1073. args->size);
  1074. if (ret)
  1075. return -EFAULT;
  1076. }
  1077. intel_runtime_pm_get(dev_priv);
  1078. ret = i915_mutex_lock_interruptible(dev);
  1079. if (ret)
  1080. goto put_rpm;
  1081. obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
  1082. if (&obj->base == NULL) {
  1083. ret = -ENOENT;
  1084. goto unlock;
  1085. }
  1086. /* Bounds check destination. */
  1087. if (args->offset > obj->base.size ||
  1088. args->size > obj->base.size - args->offset) {
  1089. ret = -EINVAL;
  1090. goto out;
  1091. }
  1092. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  1093. ret = -EFAULT;
  1094. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  1095. * it would end up going through the fenced access, and we'll get
  1096. * different detiling behavior between reading and writing.
  1097. * pread/pwrite currently are reading and writing from the CPU
  1098. * perspective, requiring manual detiling by the client.
  1099. */
  1100. if (!i915_gem_object_has_struct_page(obj) ||
  1101. cpu_write_needs_clflush(obj)) {
  1102. ret = i915_gem_gtt_pwrite_fast(dev_priv, obj, args, file);
  1103. /* Note that the gtt paths might fail with non-page-backed user
  1104. * pointers (e.g. gtt mappings when moving data between
  1105. * textures). Fallback to the shmem path in that case. */
  1106. }
  1107. if (ret == -EFAULT) {
  1108. if (obj->phys_handle)
  1109. ret = i915_gem_phys_pwrite(obj, args, file);
  1110. else if (i915_gem_object_has_struct_page(obj))
  1111. ret = i915_gem_shmem_pwrite(dev, obj, args, file);
  1112. else
  1113. ret = -ENODEV;
  1114. }
  1115. out:
  1116. drm_gem_object_unreference(&obj->base);
  1117. unlock:
  1118. mutex_unlock(&dev->struct_mutex);
  1119. put_rpm:
  1120. intel_runtime_pm_put(dev_priv);
  1121. return ret;
  1122. }
  1123. static int
  1124. i915_gem_check_wedge(unsigned reset_counter, bool interruptible)
  1125. {
  1126. if (__i915_terminally_wedged(reset_counter))
  1127. return -EIO;
  1128. if (__i915_reset_in_progress(reset_counter)) {
  1129. /* Non-interruptible callers can't handle -EAGAIN, hence return
  1130. * -EIO unconditionally for these. */
  1131. if (!interruptible)
  1132. return -EIO;
  1133. return -EAGAIN;
  1134. }
  1135. return 0;
  1136. }
  1137. static unsigned long local_clock_us(unsigned *cpu)
  1138. {
  1139. unsigned long t;
  1140. /* Cheaply and approximately convert from nanoseconds to microseconds.
  1141. * The result and subsequent calculations are also defined in the same
  1142. * approximate microseconds units. The principal source of timing
  1143. * error here is from the simple truncation.
  1144. *
  1145. * Note that local_clock() is only defined wrt to the current CPU;
  1146. * the comparisons are no longer valid if we switch CPUs. Instead of
  1147. * blocking preemption for the entire busywait, we can detect the CPU
  1148. * switch and use that as indicator of system load and a reason to
  1149. * stop busywaiting, see busywait_stop().
  1150. */
  1151. *cpu = get_cpu();
  1152. t = local_clock() >> 10;
  1153. put_cpu();
  1154. return t;
  1155. }
  1156. static bool busywait_stop(unsigned long timeout, unsigned cpu)
  1157. {
  1158. unsigned this_cpu;
  1159. if (time_after(local_clock_us(&this_cpu), timeout))
  1160. return true;
  1161. return this_cpu != cpu;
  1162. }
  1163. bool __i915_spin_request(const struct drm_i915_gem_request *req,
  1164. int state, unsigned long timeout_us)
  1165. {
  1166. unsigned cpu;
  1167. /* When waiting for high frequency requests, e.g. during synchronous
  1168. * rendering split between the CPU and GPU, the finite amount of time
  1169. * required to set up the irq and wait upon it limits the response
  1170. * rate. By busywaiting on the request completion for a short while we
  1171. * can service the high frequency waits as quick as possible. However,
  1172. * if it is a slow request, we want to sleep as quickly as possible.
  1173. * The tradeoff between waiting and sleeping is roughly the time it
  1174. * takes to sleep on a request, on the order of a microsecond.
  1175. */
  1176. timeout_us += local_clock_us(&cpu);
  1177. do {
  1178. if (i915_gem_request_completed(req))
  1179. return true;
  1180. if (signal_pending_state(state, current))
  1181. break;
  1182. if (busywait_stop(timeout_us, cpu))
  1183. break;
  1184. cpu_relax_lowlatency();
  1185. } while (!need_resched());
  1186. return false;
  1187. }
  1188. /**
  1189. * __i915_wait_request - wait until execution of request has finished
  1190. * @req: duh!
  1191. * @interruptible: do an interruptible wait (normally yes)
  1192. * @timeout: in - how long to wait (NULL forever); out - how much time remaining
  1193. * @rps: RPS client
  1194. *
  1195. * Note: It is of utmost importance that the passed in seqno and reset_counter
  1196. * values have been read by the caller in an smp safe manner. Where read-side
  1197. * locks are involved, it is sufficient to read the reset_counter before
  1198. * unlocking the lock that protects the seqno. For lockless tricks, the
  1199. * reset_counter _must_ be read before, and an appropriate smp_rmb must be
  1200. * inserted.
  1201. *
  1202. * Returns 0 if the request was found within the alloted time. Else returns the
  1203. * errno with remaining time filled in timeout argument.
  1204. */
  1205. int __i915_wait_request(struct drm_i915_gem_request *req,
  1206. bool interruptible,
  1207. s64 *timeout,
  1208. struct intel_rps_client *rps)
  1209. {
  1210. int state = interruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
  1211. DEFINE_WAIT(reset);
  1212. struct intel_wait wait;
  1213. unsigned long timeout_remain;
  1214. s64 before = 0; /* Only to silence a compiler warning. */
  1215. int ret = 0;
  1216. might_sleep();
  1217. if (list_empty(&req->list))
  1218. return 0;
  1219. if (i915_gem_request_completed(req))
  1220. return 0;
  1221. timeout_remain = MAX_SCHEDULE_TIMEOUT;
  1222. if (timeout) {
  1223. if (WARN_ON(*timeout < 0))
  1224. return -EINVAL;
  1225. if (*timeout == 0)
  1226. return -ETIME;
  1227. timeout_remain = nsecs_to_jiffies_timeout(*timeout);
  1228. /*
  1229. * Record current time in case interrupted by signal, or wedged.
  1230. */
  1231. before = ktime_get_raw_ns();
  1232. }
  1233. trace_i915_gem_request_wait_begin(req);
  1234. if (INTEL_INFO(req->i915)->gen >= 6)
  1235. gen6_rps_boost(req->i915, rps, req->emitted_jiffies);
  1236. /* Optimistic spin for the next ~jiffie before touching IRQs */
  1237. if (i915_spin_request(req, state, 5))
  1238. goto complete;
  1239. set_current_state(state);
  1240. add_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
  1241. intel_wait_init(&wait, req->seqno);
  1242. if (intel_engine_add_wait(req->engine, &wait))
  1243. /* In order to check that we haven't missed the interrupt
  1244. * as we enabled it, we need to kick ourselves to do a
  1245. * coherent check on the seqno before we sleep.
  1246. */
  1247. goto wakeup;
  1248. for (;;) {
  1249. if (signal_pending_state(state, current)) {
  1250. ret = -ERESTARTSYS;
  1251. break;
  1252. }
  1253. /* Ensure that even if the GPU hangs, we get woken up.
  1254. *
  1255. * However, note that if no one is waiting, we never notice
  1256. * a gpu hang. Eventually, we will have to wait for a resource
  1257. * held by the GPU and so trigger a hangcheck. In the most
  1258. * pathological case, this will be upon memory starvation!
  1259. */
  1260. i915_queue_hangcheck(req->i915);
  1261. timeout_remain = io_schedule_timeout(timeout_remain);
  1262. if (timeout_remain == 0) {
  1263. ret = -ETIME;
  1264. break;
  1265. }
  1266. if (intel_wait_complete(&wait))
  1267. break;
  1268. set_current_state(state);
  1269. wakeup:
  1270. /* Carefully check if the request is complete, giving time
  1271. * for the seqno to be visible following the interrupt.
  1272. * We also have to check in case we are kicked by the GPU
  1273. * reset in order to drop the struct_mutex.
  1274. */
  1275. if (__i915_request_irq_complete(req))
  1276. break;
  1277. /* Only spin if we know the GPU is processing this request */
  1278. if (i915_spin_request(req, state, 2))
  1279. break;
  1280. }
  1281. remove_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
  1282. intel_engine_remove_wait(req->engine, &wait);
  1283. __set_current_state(TASK_RUNNING);
  1284. complete:
  1285. trace_i915_gem_request_wait_end(req);
  1286. if (timeout) {
  1287. s64 tres = *timeout - (ktime_get_raw_ns() - before);
  1288. *timeout = tres < 0 ? 0 : tres;
  1289. /*
  1290. * Apparently ktime isn't accurate enough and occasionally has a
  1291. * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
  1292. * things up to make the test happy. We allow up to 1 jiffy.
  1293. *
  1294. * This is a regrssion from the timespec->ktime conversion.
  1295. */
  1296. if (ret == -ETIME && *timeout < jiffies_to_usecs(1)*1000)
  1297. *timeout = 0;
  1298. }
  1299. return ret;
  1300. }
  1301. int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
  1302. struct drm_file *file)
  1303. {
  1304. struct drm_i915_file_private *file_priv;
  1305. WARN_ON(!req || !file || req->file_priv);
  1306. if (!req || !file)
  1307. return -EINVAL;
  1308. if (req->file_priv)
  1309. return -EINVAL;
  1310. file_priv = file->driver_priv;
  1311. spin_lock(&file_priv->mm.lock);
  1312. req->file_priv = file_priv;
  1313. list_add_tail(&req->client_list, &file_priv->mm.request_list);
  1314. spin_unlock(&file_priv->mm.lock);
  1315. req->pid = get_pid(task_pid(current));
  1316. return 0;
  1317. }
  1318. static inline void
  1319. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  1320. {
  1321. struct drm_i915_file_private *file_priv = request->file_priv;
  1322. if (!file_priv)
  1323. return;
  1324. spin_lock(&file_priv->mm.lock);
  1325. list_del(&request->client_list);
  1326. request->file_priv = NULL;
  1327. spin_unlock(&file_priv->mm.lock);
  1328. put_pid(request->pid);
  1329. request->pid = NULL;
  1330. }
  1331. static void i915_gem_request_retire(struct drm_i915_gem_request *request)
  1332. {
  1333. trace_i915_gem_request_retire(request);
  1334. /* We know the GPU must have read the request to have
  1335. * sent us the seqno + interrupt, so use the position
  1336. * of tail of the request to update the last known position
  1337. * of the GPU head.
  1338. *
  1339. * Note this requires that we are always called in request
  1340. * completion order.
  1341. */
  1342. request->ringbuf->last_retired_head = request->postfix;
  1343. list_del_init(&request->list);
  1344. i915_gem_request_remove_from_client(request);
  1345. if (request->previous_context) {
  1346. if (i915.enable_execlists)
  1347. intel_lr_context_unpin(request->previous_context,
  1348. request->engine);
  1349. }
  1350. i915_gem_context_unreference(request->ctx);
  1351. i915_gem_request_unreference(request);
  1352. }
  1353. static void
  1354. __i915_gem_request_retire__upto(struct drm_i915_gem_request *req)
  1355. {
  1356. struct intel_engine_cs *engine = req->engine;
  1357. struct drm_i915_gem_request *tmp;
  1358. lockdep_assert_held(&engine->i915->dev->struct_mutex);
  1359. if (list_empty(&req->list))
  1360. return;
  1361. do {
  1362. tmp = list_first_entry(&engine->request_list,
  1363. typeof(*tmp), list);
  1364. i915_gem_request_retire(tmp);
  1365. } while (tmp != req);
  1366. WARN_ON(i915_verify_lists(engine->dev));
  1367. }
  1368. /**
  1369. * Waits for a request to be signaled, and cleans up the
  1370. * request and object lists appropriately for that event.
  1371. * @req: request to wait on
  1372. */
  1373. int
  1374. i915_wait_request(struct drm_i915_gem_request *req)
  1375. {
  1376. struct drm_i915_private *dev_priv = req->i915;
  1377. bool interruptible;
  1378. int ret;
  1379. interruptible = dev_priv->mm.interruptible;
  1380. BUG_ON(!mutex_is_locked(&dev_priv->dev->struct_mutex));
  1381. ret = __i915_wait_request(req, interruptible, NULL, NULL);
  1382. if (ret)
  1383. return ret;
  1384. /* If the GPU hung, we want to keep the requests to find the guilty. */
  1385. if (!i915_reset_in_progress(&dev_priv->gpu_error))
  1386. __i915_gem_request_retire__upto(req);
  1387. return 0;
  1388. }
  1389. /**
  1390. * Ensures that all rendering to the object has completed and the object is
  1391. * safe to unbind from the GTT or access from the CPU.
  1392. * @obj: i915 gem object
  1393. * @readonly: waiting for read access or write
  1394. */
  1395. int
  1396. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
  1397. bool readonly)
  1398. {
  1399. int ret, i;
  1400. if (!obj->active)
  1401. return 0;
  1402. if (readonly) {
  1403. if (obj->last_write_req != NULL) {
  1404. ret = i915_wait_request(obj->last_write_req);
  1405. if (ret)
  1406. return ret;
  1407. i = obj->last_write_req->engine->id;
  1408. if (obj->last_read_req[i] == obj->last_write_req)
  1409. i915_gem_object_retire__read(obj, i);
  1410. else
  1411. i915_gem_object_retire__write(obj);
  1412. }
  1413. } else {
  1414. for (i = 0; i < I915_NUM_ENGINES; i++) {
  1415. if (obj->last_read_req[i] == NULL)
  1416. continue;
  1417. ret = i915_wait_request(obj->last_read_req[i]);
  1418. if (ret)
  1419. return ret;
  1420. i915_gem_object_retire__read(obj, i);
  1421. }
  1422. GEM_BUG_ON(obj->active);
  1423. }
  1424. return 0;
  1425. }
  1426. static void
  1427. i915_gem_object_retire_request(struct drm_i915_gem_object *obj,
  1428. struct drm_i915_gem_request *req)
  1429. {
  1430. int ring = req->engine->id;
  1431. if (obj->last_read_req[ring] == req)
  1432. i915_gem_object_retire__read(obj, ring);
  1433. else if (obj->last_write_req == req)
  1434. i915_gem_object_retire__write(obj);
  1435. if (!i915_reset_in_progress(&req->i915->gpu_error))
  1436. __i915_gem_request_retire__upto(req);
  1437. }
  1438. /* A nonblocking variant of the above wait. This is a highly dangerous routine
  1439. * as the object state may change during this call.
  1440. */
  1441. static __must_check int
  1442. i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
  1443. struct intel_rps_client *rps,
  1444. bool readonly)
  1445. {
  1446. struct drm_device *dev = obj->base.dev;
  1447. struct drm_i915_private *dev_priv = dev->dev_private;
  1448. struct drm_i915_gem_request *requests[I915_NUM_ENGINES];
  1449. int ret, i, n = 0;
  1450. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1451. BUG_ON(!dev_priv->mm.interruptible);
  1452. if (!obj->active)
  1453. return 0;
  1454. if (readonly) {
  1455. struct drm_i915_gem_request *req;
  1456. req = obj->last_write_req;
  1457. if (req == NULL)
  1458. return 0;
  1459. requests[n++] = i915_gem_request_reference(req);
  1460. } else {
  1461. for (i = 0; i < I915_NUM_ENGINES; i++) {
  1462. struct drm_i915_gem_request *req;
  1463. req = obj->last_read_req[i];
  1464. if (req == NULL)
  1465. continue;
  1466. requests[n++] = i915_gem_request_reference(req);
  1467. }
  1468. }
  1469. mutex_unlock(&dev->struct_mutex);
  1470. ret = 0;
  1471. for (i = 0; ret == 0 && i < n; i++)
  1472. ret = __i915_wait_request(requests[i], true, NULL, rps);
  1473. mutex_lock(&dev->struct_mutex);
  1474. for (i = 0; i < n; i++) {
  1475. if (ret == 0)
  1476. i915_gem_object_retire_request(obj, requests[i]);
  1477. i915_gem_request_unreference(requests[i]);
  1478. }
  1479. return ret;
  1480. }
  1481. static struct intel_rps_client *to_rps_client(struct drm_file *file)
  1482. {
  1483. struct drm_i915_file_private *fpriv = file->driver_priv;
  1484. return &fpriv->rps;
  1485. }
  1486. static enum fb_op_origin
  1487. write_origin(struct drm_i915_gem_object *obj, unsigned domain)
  1488. {
  1489. return domain == I915_GEM_DOMAIN_GTT && !obj->has_wc_mmap ?
  1490. ORIGIN_GTT : ORIGIN_CPU;
  1491. }
  1492. /**
  1493. * Called when user space prepares to use an object with the CPU, either
  1494. * through the mmap ioctl's mapping or a GTT mapping.
  1495. * @dev: drm device
  1496. * @data: ioctl data blob
  1497. * @file: drm file
  1498. */
  1499. int
  1500. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  1501. struct drm_file *file)
  1502. {
  1503. struct drm_i915_gem_set_domain *args = data;
  1504. struct drm_i915_gem_object *obj;
  1505. uint32_t read_domains = args->read_domains;
  1506. uint32_t write_domain = args->write_domain;
  1507. int ret;
  1508. /* Only handle setting domains to types used by the CPU. */
  1509. if (write_domain & I915_GEM_GPU_DOMAINS)
  1510. return -EINVAL;
  1511. if (read_domains & I915_GEM_GPU_DOMAINS)
  1512. return -EINVAL;
  1513. /* Having something in the write domain implies it's in the read
  1514. * domain, and only that read domain. Enforce that in the request.
  1515. */
  1516. if (write_domain != 0 && read_domains != write_domain)
  1517. return -EINVAL;
  1518. ret = i915_mutex_lock_interruptible(dev);
  1519. if (ret)
  1520. return ret;
  1521. obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
  1522. if (&obj->base == NULL) {
  1523. ret = -ENOENT;
  1524. goto unlock;
  1525. }
  1526. /* Try to flush the object off the GPU without holding the lock.
  1527. * We will repeat the flush holding the lock in the normal manner
  1528. * to catch cases where we are gazumped.
  1529. */
  1530. ret = i915_gem_object_wait_rendering__nonblocking(obj,
  1531. to_rps_client(file),
  1532. !write_domain);
  1533. if (ret)
  1534. goto unref;
  1535. if (read_domains & I915_GEM_DOMAIN_GTT)
  1536. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  1537. else
  1538. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  1539. if (write_domain != 0)
  1540. intel_fb_obj_invalidate(obj, write_origin(obj, write_domain));
  1541. unref:
  1542. drm_gem_object_unreference(&obj->base);
  1543. unlock:
  1544. mutex_unlock(&dev->struct_mutex);
  1545. return ret;
  1546. }
  1547. /**
  1548. * Called when user space has done writes to this buffer
  1549. * @dev: drm device
  1550. * @data: ioctl data blob
  1551. * @file: drm file
  1552. */
  1553. int
  1554. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1555. struct drm_file *file)
  1556. {
  1557. struct drm_i915_gem_sw_finish *args = data;
  1558. struct drm_i915_gem_object *obj;
  1559. int ret = 0;
  1560. ret = i915_mutex_lock_interruptible(dev);
  1561. if (ret)
  1562. return ret;
  1563. obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
  1564. if (&obj->base == NULL) {
  1565. ret = -ENOENT;
  1566. goto unlock;
  1567. }
  1568. /* Pinned buffers may be scanout, so flush the cache */
  1569. if (obj->pin_display)
  1570. i915_gem_object_flush_cpu_write_domain(obj);
  1571. drm_gem_object_unreference(&obj->base);
  1572. unlock:
  1573. mutex_unlock(&dev->struct_mutex);
  1574. return ret;
  1575. }
  1576. /**
  1577. * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
  1578. * it is mapped to.
  1579. * @dev: drm device
  1580. * @data: ioctl data blob
  1581. * @file: drm file
  1582. *
  1583. * While the mapping holds a reference on the contents of the object, it doesn't
  1584. * imply a ref on the object itself.
  1585. *
  1586. * IMPORTANT:
  1587. *
  1588. * DRM driver writers who look a this function as an example for how to do GEM
  1589. * mmap support, please don't implement mmap support like here. The modern way
  1590. * to implement DRM mmap support is with an mmap offset ioctl (like
  1591. * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
  1592. * That way debug tooling like valgrind will understand what's going on, hiding
  1593. * the mmap call in a driver private ioctl will break that. The i915 driver only
  1594. * does cpu mmaps this way because we didn't know better.
  1595. */
  1596. int
  1597. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1598. struct drm_file *file)
  1599. {
  1600. struct drm_i915_gem_mmap *args = data;
  1601. struct drm_gem_object *obj;
  1602. unsigned long addr;
  1603. if (args->flags & ~(I915_MMAP_WC))
  1604. return -EINVAL;
  1605. if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
  1606. return -ENODEV;
  1607. obj = drm_gem_object_lookup(file, args->handle);
  1608. if (obj == NULL)
  1609. return -ENOENT;
  1610. /* prime objects have no backing filp to GEM mmap
  1611. * pages from.
  1612. */
  1613. if (!obj->filp) {
  1614. drm_gem_object_unreference_unlocked(obj);
  1615. return -EINVAL;
  1616. }
  1617. addr = vm_mmap(obj->filp, 0, args->size,
  1618. PROT_READ | PROT_WRITE, MAP_SHARED,
  1619. args->offset);
  1620. if (args->flags & I915_MMAP_WC) {
  1621. struct mm_struct *mm = current->mm;
  1622. struct vm_area_struct *vma;
  1623. if (down_write_killable(&mm->mmap_sem)) {
  1624. drm_gem_object_unreference_unlocked(obj);
  1625. return -EINTR;
  1626. }
  1627. vma = find_vma(mm, addr);
  1628. if (vma)
  1629. vma->vm_page_prot =
  1630. pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  1631. else
  1632. addr = -ENOMEM;
  1633. up_write(&mm->mmap_sem);
  1634. /* This may race, but that's ok, it only gets set */
  1635. WRITE_ONCE(to_intel_bo(obj)->has_wc_mmap, true);
  1636. }
  1637. drm_gem_object_unreference_unlocked(obj);
  1638. if (IS_ERR((void *)addr))
  1639. return addr;
  1640. args->addr_ptr = (uint64_t) addr;
  1641. return 0;
  1642. }
  1643. /**
  1644. * i915_gem_fault - fault a page into the GTT
  1645. * @vma: VMA in question
  1646. * @vmf: fault info
  1647. *
  1648. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  1649. * from userspace. The fault handler takes care of binding the object to
  1650. * the GTT (if needed), allocating and programming a fence register (again,
  1651. * only if needed based on whether the old reg is still valid or the object
  1652. * is tiled) and inserting a new PTE into the faulting process.
  1653. *
  1654. * Note that the faulting process may involve evicting existing objects
  1655. * from the GTT and/or fence registers to make room. So performance may
  1656. * suffer if the GTT working set is large or there are few fence registers
  1657. * left.
  1658. */
  1659. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1660. {
  1661. struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
  1662. struct drm_device *dev = obj->base.dev;
  1663. struct drm_i915_private *dev_priv = to_i915(dev);
  1664. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1665. struct i915_ggtt_view view = i915_ggtt_view_normal;
  1666. pgoff_t page_offset;
  1667. unsigned long pfn;
  1668. int ret = 0;
  1669. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  1670. intel_runtime_pm_get(dev_priv);
  1671. /* We don't use vmf->pgoff since that has the fake offset */
  1672. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  1673. PAGE_SHIFT;
  1674. ret = i915_mutex_lock_interruptible(dev);
  1675. if (ret)
  1676. goto out;
  1677. trace_i915_gem_object_fault(obj, page_offset, true, write);
  1678. /* Try to flush the object off the GPU first without holding the lock.
  1679. * Upon reacquiring the lock, we will perform our sanity checks and then
  1680. * repeat the flush holding the lock in the normal manner to catch cases
  1681. * where we are gazumped.
  1682. */
  1683. ret = i915_gem_object_wait_rendering__nonblocking(obj, NULL, !write);
  1684. if (ret)
  1685. goto unlock;
  1686. /* Access to snoopable pages through the GTT is incoherent. */
  1687. if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
  1688. ret = -EFAULT;
  1689. goto unlock;
  1690. }
  1691. /* Use a partial view if the object is bigger than the aperture. */
  1692. if (obj->base.size >= ggtt->mappable_end &&
  1693. obj->tiling_mode == I915_TILING_NONE) {
  1694. static const unsigned int chunk_size = 256; // 1 MiB
  1695. memset(&view, 0, sizeof(view));
  1696. view.type = I915_GGTT_VIEW_PARTIAL;
  1697. view.params.partial.offset = rounddown(page_offset, chunk_size);
  1698. view.params.partial.size =
  1699. min_t(unsigned int,
  1700. chunk_size,
  1701. (vma->vm_end - vma->vm_start)/PAGE_SIZE -
  1702. view.params.partial.offset);
  1703. }
  1704. /* Now pin it into the GTT if needed */
  1705. ret = i915_gem_object_ggtt_pin(obj, &view, 0, PIN_MAPPABLE);
  1706. if (ret)
  1707. goto unlock;
  1708. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1709. if (ret)
  1710. goto unpin;
  1711. ret = i915_gem_object_get_fence(obj);
  1712. if (ret)
  1713. goto unpin;
  1714. /* Finally, remap it using the new GTT offset */
  1715. pfn = ggtt->mappable_base +
  1716. i915_gem_obj_ggtt_offset_view(obj, &view);
  1717. pfn >>= PAGE_SHIFT;
  1718. if (unlikely(view.type == I915_GGTT_VIEW_PARTIAL)) {
  1719. /* Overriding existing pages in partial view does not cause
  1720. * us any trouble as TLBs are still valid because the fault
  1721. * is due to userspace losing part of the mapping or never
  1722. * having accessed it before (at this partials' range).
  1723. */
  1724. unsigned long base = vma->vm_start +
  1725. (view.params.partial.offset << PAGE_SHIFT);
  1726. unsigned int i;
  1727. for (i = 0; i < view.params.partial.size; i++) {
  1728. ret = vm_insert_pfn(vma, base + i * PAGE_SIZE, pfn + i);
  1729. if (ret)
  1730. break;
  1731. }
  1732. obj->fault_mappable = true;
  1733. } else {
  1734. if (!obj->fault_mappable) {
  1735. unsigned long size = min_t(unsigned long,
  1736. vma->vm_end - vma->vm_start,
  1737. obj->base.size);
  1738. int i;
  1739. for (i = 0; i < size >> PAGE_SHIFT; i++) {
  1740. ret = vm_insert_pfn(vma,
  1741. (unsigned long)vma->vm_start + i * PAGE_SIZE,
  1742. pfn + i);
  1743. if (ret)
  1744. break;
  1745. }
  1746. obj->fault_mappable = true;
  1747. } else
  1748. ret = vm_insert_pfn(vma,
  1749. (unsigned long)vmf->virtual_address,
  1750. pfn + page_offset);
  1751. }
  1752. unpin:
  1753. i915_gem_object_ggtt_unpin_view(obj, &view);
  1754. unlock:
  1755. mutex_unlock(&dev->struct_mutex);
  1756. out:
  1757. switch (ret) {
  1758. case -EIO:
  1759. /*
  1760. * We eat errors when the gpu is terminally wedged to avoid
  1761. * userspace unduly crashing (gl has no provisions for mmaps to
  1762. * fail). But any other -EIO isn't ours (e.g. swap in failure)
  1763. * and so needs to be reported.
  1764. */
  1765. if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
  1766. ret = VM_FAULT_SIGBUS;
  1767. break;
  1768. }
  1769. case -EAGAIN:
  1770. /*
  1771. * EAGAIN means the gpu is hung and we'll wait for the error
  1772. * handler to reset everything when re-faulting in
  1773. * i915_mutex_lock_interruptible.
  1774. */
  1775. case 0:
  1776. case -ERESTARTSYS:
  1777. case -EINTR:
  1778. case -EBUSY:
  1779. /*
  1780. * EBUSY is ok: this just means that another thread
  1781. * already did the job.
  1782. */
  1783. ret = VM_FAULT_NOPAGE;
  1784. break;
  1785. case -ENOMEM:
  1786. ret = VM_FAULT_OOM;
  1787. break;
  1788. case -ENOSPC:
  1789. case -EFAULT:
  1790. ret = VM_FAULT_SIGBUS;
  1791. break;
  1792. default:
  1793. WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
  1794. ret = VM_FAULT_SIGBUS;
  1795. break;
  1796. }
  1797. intel_runtime_pm_put(dev_priv);
  1798. return ret;
  1799. }
  1800. /**
  1801. * i915_gem_release_mmap - remove physical page mappings
  1802. * @obj: obj in question
  1803. *
  1804. * Preserve the reservation of the mmapping with the DRM core code, but
  1805. * relinquish ownership of the pages back to the system.
  1806. *
  1807. * It is vital that we remove the page mapping if we have mapped a tiled
  1808. * object through the GTT and then lose the fence register due to
  1809. * resource pressure. Similarly if the object has been moved out of the
  1810. * aperture, than pages mapped into userspace must be revoked. Removing the
  1811. * mapping will then trigger a page fault on the next user access, allowing
  1812. * fixup by i915_gem_fault().
  1813. */
  1814. void
  1815. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1816. {
  1817. /* Serialisation between user GTT access and our code depends upon
  1818. * revoking the CPU's PTE whilst the mutex is held. The next user
  1819. * pagefault then has to wait until we release the mutex.
  1820. */
  1821. lockdep_assert_held(&obj->base.dev->struct_mutex);
  1822. if (!obj->fault_mappable)
  1823. return;
  1824. drm_vma_node_unmap(&obj->base.vma_node,
  1825. obj->base.dev->anon_inode->i_mapping);
  1826. /* Ensure that the CPU's PTE are revoked and there are not outstanding
  1827. * memory transactions from userspace before we return. The TLB
  1828. * flushing implied above by changing the PTE above *should* be
  1829. * sufficient, an extra barrier here just provides us with a bit
  1830. * of paranoid documentation about our requirement to serialise
  1831. * memory writes before touching registers / GSM.
  1832. */
  1833. wmb();
  1834. obj->fault_mappable = false;
  1835. }
  1836. void
  1837. i915_gem_release_all_mmaps(struct drm_i915_private *dev_priv)
  1838. {
  1839. struct drm_i915_gem_object *obj;
  1840. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
  1841. i915_gem_release_mmap(obj);
  1842. }
  1843. uint32_t
  1844. i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
  1845. {
  1846. uint32_t gtt_size;
  1847. if (INTEL_INFO(dev)->gen >= 4 ||
  1848. tiling_mode == I915_TILING_NONE)
  1849. return size;
  1850. /* Previous chips need a power-of-two fence region when tiling */
  1851. if (IS_GEN3(dev))
  1852. gtt_size = 1024*1024;
  1853. else
  1854. gtt_size = 512*1024;
  1855. while (gtt_size < size)
  1856. gtt_size <<= 1;
  1857. return gtt_size;
  1858. }
  1859. /**
  1860. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1861. * @dev: drm device
  1862. * @size: object size
  1863. * @tiling_mode: tiling mode
  1864. * @fenced: is fenced alignemned required or not
  1865. *
  1866. * Return the required GTT alignment for an object, taking into account
  1867. * potential fence register mapping.
  1868. */
  1869. uint32_t
  1870. i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
  1871. int tiling_mode, bool fenced)
  1872. {
  1873. /*
  1874. * Minimum alignment is 4k (GTT page size), but might be greater
  1875. * if a fence register is needed for the object.
  1876. */
  1877. if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
  1878. tiling_mode == I915_TILING_NONE)
  1879. return 4096;
  1880. /*
  1881. * Previous chips need to be aligned to the size of the smallest
  1882. * fence register that can contain the object.
  1883. */
  1884. return i915_gem_get_gtt_size(dev, size, tiling_mode);
  1885. }
  1886. static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
  1887. {
  1888. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1889. int ret;
  1890. dev_priv->mm.shrinker_no_lock_stealing = true;
  1891. ret = drm_gem_create_mmap_offset(&obj->base);
  1892. if (ret != -ENOSPC)
  1893. goto out;
  1894. /* Badly fragmented mmap space? The only way we can recover
  1895. * space is by destroying unwanted objects. We can't randomly release
  1896. * mmap_offsets as userspace expects them to be persistent for the
  1897. * lifetime of the objects. The closest we can is to release the
  1898. * offsets on purgeable objects by truncating it and marking it purged,
  1899. * which prevents userspace from ever using that object again.
  1900. */
  1901. i915_gem_shrink(dev_priv,
  1902. obj->base.size >> PAGE_SHIFT,
  1903. I915_SHRINK_BOUND |
  1904. I915_SHRINK_UNBOUND |
  1905. I915_SHRINK_PURGEABLE);
  1906. ret = drm_gem_create_mmap_offset(&obj->base);
  1907. if (ret != -ENOSPC)
  1908. goto out;
  1909. i915_gem_shrink_all(dev_priv);
  1910. ret = drm_gem_create_mmap_offset(&obj->base);
  1911. out:
  1912. dev_priv->mm.shrinker_no_lock_stealing = false;
  1913. return ret;
  1914. }
  1915. static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
  1916. {
  1917. drm_gem_free_mmap_offset(&obj->base);
  1918. }
  1919. int
  1920. i915_gem_mmap_gtt(struct drm_file *file,
  1921. struct drm_device *dev,
  1922. uint32_t handle,
  1923. uint64_t *offset)
  1924. {
  1925. struct drm_i915_gem_object *obj;
  1926. int ret;
  1927. ret = i915_mutex_lock_interruptible(dev);
  1928. if (ret)
  1929. return ret;
  1930. obj = to_intel_bo(drm_gem_object_lookup(file, handle));
  1931. if (&obj->base == NULL) {
  1932. ret = -ENOENT;
  1933. goto unlock;
  1934. }
  1935. if (obj->madv != I915_MADV_WILLNEED) {
  1936. DRM_DEBUG("Attempting to mmap a purgeable buffer\n");
  1937. ret = -EFAULT;
  1938. goto out;
  1939. }
  1940. ret = i915_gem_object_create_mmap_offset(obj);
  1941. if (ret)
  1942. goto out;
  1943. *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
  1944. out:
  1945. drm_gem_object_unreference(&obj->base);
  1946. unlock:
  1947. mutex_unlock(&dev->struct_mutex);
  1948. return ret;
  1949. }
  1950. /**
  1951. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1952. * @dev: DRM device
  1953. * @data: GTT mapping ioctl data
  1954. * @file: GEM object info
  1955. *
  1956. * Simply returns the fake offset to userspace so it can mmap it.
  1957. * The mmap call will end up in drm_gem_mmap(), which will set things
  1958. * up so we can get faults in the handler above.
  1959. *
  1960. * The fault handler will take care of binding the object into the GTT
  1961. * (since it may have been evicted to make room for something), allocating
  1962. * a fence register, and mapping the appropriate aperture address into
  1963. * userspace.
  1964. */
  1965. int
  1966. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1967. struct drm_file *file)
  1968. {
  1969. struct drm_i915_gem_mmap_gtt *args = data;
  1970. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1971. }
  1972. /* Immediately discard the backing storage */
  1973. static void
  1974. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1975. {
  1976. i915_gem_object_free_mmap_offset(obj);
  1977. if (obj->base.filp == NULL)
  1978. return;
  1979. /* Our goal here is to return as much of the memory as
  1980. * is possible back to the system as we are called from OOM.
  1981. * To do this we must instruct the shmfs to drop all of its
  1982. * backing pages, *now*.
  1983. */
  1984. shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
  1985. obj->madv = __I915_MADV_PURGED;
  1986. }
  1987. /* Try to discard unwanted pages */
  1988. static void
  1989. i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
  1990. {
  1991. struct address_space *mapping;
  1992. switch (obj->madv) {
  1993. case I915_MADV_DONTNEED:
  1994. i915_gem_object_truncate(obj);
  1995. case __I915_MADV_PURGED:
  1996. return;
  1997. }
  1998. if (obj->base.filp == NULL)
  1999. return;
  2000. mapping = file_inode(obj->base.filp)->i_mapping,
  2001. invalidate_mapping_pages(mapping, 0, (loff_t)-1);
  2002. }
  2003. static void
  2004. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
  2005. {
  2006. struct sgt_iter sgt_iter;
  2007. struct page *page;
  2008. int ret;
  2009. BUG_ON(obj->madv == __I915_MADV_PURGED);
  2010. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  2011. if (WARN_ON(ret)) {
  2012. /* In the event of a disaster, abandon all caches and
  2013. * hope for the best.
  2014. */
  2015. i915_gem_clflush_object(obj, true);
  2016. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2017. }
  2018. i915_gem_gtt_finish_object(obj);
  2019. if (i915_gem_object_needs_bit17_swizzle(obj))
  2020. i915_gem_object_save_bit_17_swizzle(obj);
  2021. if (obj->madv == I915_MADV_DONTNEED)
  2022. obj->dirty = 0;
  2023. for_each_sgt_page(page, sgt_iter, obj->pages) {
  2024. if (obj->dirty)
  2025. set_page_dirty(page);
  2026. if (obj->madv == I915_MADV_WILLNEED)
  2027. mark_page_accessed(page);
  2028. put_page(page);
  2029. }
  2030. obj->dirty = 0;
  2031. sg_free_table(obj->pages);
  2032. kfree(obj->pages);
  2033. }
  2034. int
  2035. i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
  2036. {
  2037. const struct drm_i915_gem_object_ops *ops = obj->ops;
  2038. if (obj->pages == NULL)
  2039. return 0;
  2040. if (obj->pages_pin_count)
  2041. return -EBUSY;
  2042. BUG_ON(i915_gem_obj_bound_any(obj));
  2043. /* ->put_pages might need to allocate memory for the bit17 swizzle
  2044. * array, hence protect them from being reaped by removing them from gtt
  2045. * lists early. */
  2046. list_del(&obj->global_list);
  2047. if (obj->mapping) {
  2048. if (is_vmalloc_addr(obj->mapping))
  2049. vunmap(obj->mapping);
  2050. else
  2051. kunmap(kmap_to_page(obj->mapping));
  2052. obj->mapping = NULL;
  2053. }
  2054. ops->put_pages(obj);
  2055. obj->pages = NULL;
  2056. i915_gem_object_invalidate(obj);
  2057. return 0;
  2058. }
  2059. static int
  2060. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  2061. {
  2062. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2063. int page_count, i;
  2064. struct address_space *mapping;
  2065. struct sg_table *st;
  2066. struct scatterlist *sg;
  2067. struct sgt_iter sgt_iter;
  2068. struct page *page;
  2069. unsigned long last_pfn = 0; /* suppress gcc warning */
  2070. int ret;
  2071. gfp_t gfp;
  2072. /* Assert that the object is not currently in any GPU domain. As it
  2073. * wasn't in the GTT, there shouldn't be any way it could have been in
  2074. * a GPU cache
  2075. */
  2076. BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  2077. BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  2078. st = kmalloc(sizeof(*st), GFP_KERNEL);
  2079. if (st == NULL)
  2080. return -ENOMEM;
  2081. page_count = obj->base.size / PAGE_SIZE;
  2082. if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  2083. kfree(st);
  2084. return -ENOMEM;
  2085. }
  2086. /* Get the list of pages out of our struct file. They'll be pinned
  2087. * at this point until we release them.
  2088. *
  2089. * Fail silently without starting the shrinker
  2090. */
  2091. mapping = file_inode(obj->base.filp)->i_mapping;
  2092. gfp = mapping_gfp_constraint(mapping, ~(__GFP_IO | __GFP_RECLAIM));
  2093. gfp |= __GFP_NORETRY | __GFP_NOWARN;
  2094. sg = st->sgl;
  2095. st->nents = 0;
  2096. for (i = 0; i < page_count; i++) {
  2097. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  2098. if (IS_ERR(page)) {
  2099. i915_gem_shrink(dev_priv,
  2100. page_count,
  2101. I915_SHRINK_BOUND |
  2102. I915_SHRINK_UNBOUND |
  2103. I915_SHRINK_PURGEABLE);
  2104. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  2105. }
  2106. if (IS_ERR(page)) {
  2107. /* We've tried hard to allocate the memory by reaping
  2108. * our own buffer, now let the real VM do its job and
  2109. * go down in flames if truly OOM.
  2110. */
  2111. i915_gem_shrink_all(dev_priv);
  2112. page = shmem_read_mapping_page(mapping, i);
  2113. if (IS_ERR(page)) {
  2114. ret = PTR_ERR(page);
  2115. goto err_pages;
  2116. }
  2117. }
  2118. #ifdef CONFIG_SWIOTLB
  2119. if (swiotlb_nr_tbl()) {
  2120. st->nents++;
  2121. sg_set_page(sg, page, PAGE_SIZE, 0);
  2122. sg = sg_next(sg);
  2123. continue;
  2124. }
  2125. #endif
  2126. if (!i || page_to_pfn(page) != last_pfn + 1) {
  2127. if (i)
  2128. sg = sg_next(sg);
  2129. st->nents++;
  2130. sg_set_page(sg, page, PAGE_SIZE, 0);
  2131. } else {
  2132. sg->length += PAGE_SIZE;
  2133. }
  2134. last_pfn = page_to_pfn(page);
  2135. /* Check that the i965g/gm workaround works. */
  2136. WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
  2137. }
  2138. #ifdef CONFIG_SWIOTLB
  2139. if (!swiotlb_nr_tbl())
  2140. #endif
  2141. sg_mark_end(sg);
  2142. obj->pages = st;
  2143. ret = i915_gem_gtt_prepare_object(obj);
  2144. if (ret)
  2145. goto err_pages;
  2146. if (i915_gem_object_needs_bit17_swizzle(obj))
  2147. i915_gem_object_do_bit_17_swizzle(obj);
  2148. if (obj->tiling_mode != I915_TILING_NONE &&
  2149. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
  2150. i915_gem_object_pin_pages(obj);
  2151. return 0;
  2152. err_pages:
  2153. sg_mark_end(sg);
  2154. for_each_sgt_page(page, sgt_iter, st)
  2155. put_page(page);
  2156. sg_free_table(st);
  2157. kfree(st);
  2158. /* shmemfs first checks if there is enough memory to allocate the page
  2159. * and reports ENOSPC should there be insufficient, along with the usual
  2160. * ENOMEM for a genuine allocation failure.
  2161. *
  2162. * We use ENOSPC in our driver to mean that we have run out of aperture
  2163. * space and so want to translate the error from shmemfs back to our
  2164. * usual understanding of ENOMEM.
  2165. */
  2166. if (ret == -ENOSPC)
  2167. ret = -ENOMEM;
  2168. return ret;
  2169. }
  2170. /* Ensure that the associated pages are gathered from the backing storage
  2171. * and pinned into our object. i915_gem_object_get_pages() may be called
  2172. * multiple times before they are released by a single call to
  2173. * i915_gem_object_put_pages() - once the pages are no longer referenced
  2174. * either as a result of memory pressure (reaping pages under the shrinker)
  2175. * or as the object is itself released.
  2176. */
  2177. int
  2178. i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  2179. {
  2180. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2181. const struct drm_i915_gem_object_ops *ops = obj->ops;
  2182. int ret;
  2183. if (obj->pages)
  2184. return 0;
  2185. if (obj->madv != I915_MADV_WILLNEED) {
  2186. DRM_DEBUG("Attempting to obtain a purgeable object\n");
  2187. return -EFAULT;
  2188. }
  2189. BUG_ON(obj->pages_pin_count);
  2190. ret = ops->get_pages(obj);
  2191. if (ret)
  2192. return ret;
  2193. list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  2194. obj->get_page.sg = obj->pages->sgl;
  2195. obj->get_page.last = 0;
  2196. return 0;
  2197. }
  2198. /* The 'mapping' part of i915_gem_object_pin_map() below */
  2199. static void *i915_gem_object_map(const struct drm_i915_gem_object *obj)
  2200. {
  2201. unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
  2202. struct sg_table *sgt = obj->pages;
  2203. struct sgt_iter sgt_iter;
  2204. struct page *page;
  2205. struct page *stack_pages[32];
  2206. struct page **pages = stack_pages;
  2207. unsigned long i = 0;
  2208. void *addr;
  2209. /* A single page can always be kmapped */
  2210. if (n_pages == 1)
  2211. return kmap(sg_page(sgt->sgl));
  2212. if (n_pages > ARRAY_SIZE(stack_pages)) {
  2213. /* Too big for stack -- allocate temporary array instead */
  2214. pages = drm_malloc_gfp(n_pages, sizeof(*pages), GFP_TEMPORARY);
  2215. if (!pages)
  2216. return NULL;
  2217. }
  2218. for_each_sgt_page(page, sgt_iter, sgt)
  2219. pages[i++] = page;
  2220. /* Check that we have the expected number of pages */
  2221. GEM_BUG_ON(i != n_pages);
  2222. addr = vmap(pages, n_pages, 0, PAGE_KERNEL);
  2223. if (pages != stack_pages)
  2224. drm_free_large(pages);
  2225. return addr;
  2226. }
  2227. /* get, pin, and map the pages of the object into kernel space */
  2228. void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj)
  2229. {
  2230. int ret;
  2231. lockdep_assert_held(&obj->base.dev->struct_mutex);
  2232. ret = i915_gem_object_get_pages(obj);
  2233. if (ret)
  2234. return ERR_PTR(ret);
  2235. i915_gem_object_pin_pages(obj);
  2236. if (!obj->mapping) {
  2237. obj->mapping = i915_gem_object_map(obj);
  2238. if (!obj->mapping) {
  2239. i915_gem_object_unpin_pages(obj);
  2240. return ERR_PTR(-ENOMEM);
  2241. }
  2242. }
  2243. return obj->mapping;
  2244. }
  2245. void i915_vma_move_to_active(struct i915_vma *vma,
  2246. struct drm_i915_gem_request *req)
  2247. {
  2248. struct drm_i915_gem_object *obj = vma->obj;
  2249. struct intel_engine_cs *engine;
  2250. engine = i915_gem_request_get_engine(req);
  2251. /* Add a reference if we're newly entering the active list. */
  2252. if (obj->active == 0)
  2253. drm_gem_object_reference(&obj->base);
  2254. obj->active |= intel_engine_flag(engine);
  2255. list_move_tail(&obj->engine_list[engine->id], &engine->active_list);
  2256. i915_gem_request_assign(&obj->last_read_req[engine->id], req);
  2257. list_move_tail(&vma->vm_link, &vma->vm->active_list);
  2258. }
  2259. static void
  2260. i915_gem_object_retire__write(struct drm_i915_gem_object *obj)
  2261. {
  2262. GEM_BUG_ON(obj->last_write_req == NULL);
  2263. GEM_BUG_ON(!(obj->active & intel_engine_flag(obj->last_write_req->engine)));
  2264. i915_gem_request_assign(&obj->last_write_req, NULL);
  2265. intel_fb_obj_flush(obj, true, ORIGIN_CS);
  2266. }
  2267. static void
  2268. i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int ring)
  2269. {
  2270. struct i915_vma *vma;
  2271. GEM_BUG_ON(obj->last_read_req[ring] == NULL);
  2272. GEM_BUG_ON(!(obj->active & (1 << ring)));
  2273. list_del_init(&obj->engine_list[ring]);
  2274. i915_gem_request_assign(&obj->last_read_req[ring], NULL);
  2275. if (obj->last_write_req && obj->last_write_req->engine->id == ring)
  2276. i915_gem_object_retire__write(obj);
  2277. obj->active &= ~(1 << ring);
  2278. if (obj->active)
  2279. return;
  2280. /* Bump our place on the bound list to keep it roughly in LRU order
  2281. * so that we don't steal from recently used but inactive objects
  2282. * (unless we are forced to ofc!)
  2283. */
  2284. list_move_tail(&obj->global_list,
  2285. &to_i915(obj->base.dev)->mm.bound_list);
  2286. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  2287. if (!list_empty(&vma->vm_link))
  2288. list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
  2289. }
  2290. i915_gem_request_assign(&obj->last_fenced_req, NULL);
  2291. drm_gem_object_unreference(&obj->base);
  2292. }
  2293. static int
  2294. i915_gem_init_seqno(struct drm_i915_private *dev_priv, u32 seqno)
  2295. {
  2296. struct intel_engine_cs *engine;
  2297. int ret;
  2298. /* Carefully retire all requests without writing to the rings */
  2299. for_each_engine(engine, dev_priv) {
  2300. ret = intel_engine_idle(engine);
  2301. if (ret)
  2302. return ret;
  2303. }
  2304. i915_gem_retire_requests(dev_priv);
  2305. /* If the seqno wraps around, we need to clear the breadcrumb rbtree */
  2306. if (!i915_seqno_passed(seqno, dev_priv->next_seqno)) {
  2307. while (intel_kick_waiters(dev_priv))
  2308. yield();
  2309. }
  2310. /* Finally reset hw state */
  2311. for_each_engine(engine, dev_priv)
  2312. intel_ring_init_seqno(engine, seqno);
  2313. return 0;
  2314. }
  2315. int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
  2316. {
  2317. struct drm_i915_private *dev_priv = dev->dev_private;
  2318. int ret;
  2319. if (seqno == 0)
  2320. return -EINVAL;
  2321. /* HWS page needs to be set less than what we
  2322. * will inject to ring
  2323. */
  2324. ret = i915_gem_init_seqno(dev_priv, seqno - 1);
  2325. if (ret)
  2326. return ret;
  2327. /* Carefully set the last_seqno value so that wrap
  2328. * detection still works
  2329. */
  2330. dev_priv->next_seqno = seqno;
  2331. dev_priv->last_seqno = seqno - 1;
  2332. if (dev_priv->last_seqno == 0)
  2333. dev_priv->last_seqno--;
  2334. return 0;
  2335. }
  2336. int
  2337. i915_gem_get_seqno(struct drm_i915_private *dev_priv, u32 *seqno)
  2338. {
  2339. /* reserve 0 for non-seqno */
  2340. if (dev_priv->next_seqno == 0) {
  2341. int ret = i915_gem_init_seqno(dev_priv, 0);
  2342. if (ret)
  2343. return ret;
  2344. dev_priv->next_seqno = 1;
  2345. }
  2346. *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
  2347. return 0;
  2348. }
  2349. /*
  2350. * NB: This function is not allowed to fail. Doing so would mean the the
  2351. * request is not being tracked for completion but the work itself is
  2352. * going to happen on the hardware. This would be a Bad Thing(tm).
  2353. */
  2354. void __i915_add_request(struct drm_i915_gem_request *request,
  2355. struct drm_i915_gem_object *obj,
  2356. bool flush_caches)
  2357. {
  2358. struct intel_engine_cs *engine;
  2359. struct drm_i915_private *dev_priv;
  2360. struct intel_ringbuffer *ringbuf;
  2361. u32 request_start;
  2362. u32 reserved_tail;
  2363. int ret;
  2364. if (WARN_ON(request == NULL))
  2365. return;
  2366. engine = request->engine;
  2367. dev_priv = request->i915;
  2368. ringbuf = request->ringbuf;
  2369. /*
  2370. * To ensure that this call will not fail, space for its emissions
  2371. * should already have been reserved in the ring buffer. Let the ring
  2372. * know that it is time to use that space up.
  2373. */
  2374. request_start = intel_ring_get_tail(ringbuf);
  2375. reserved_tail = request->reserved_space;
  2376. request->reserved_space = 0;
  2377. /*
  2378. * Emit any outstanding flushes - execbuf can fail to emit the flush
  2379. * after having emitted the batchbuffer command. Hence we need to fix
  2380. * things up similar to emitting the lazy request. The difference here
  2381. * is that the flush _must_ happen before the next request, no matter
  2382. * what.
  2383. */
  2384. if (flush_caches) {
  2385. if (i915.enable_execlists)
  2386. ret = logical_ring_flush_all_caches(request);
  2387. else
  2388. ret = intel_ring_flush_all_caches(request);
  2389. /* Not allowed to fail! */
  2390. WARN(ret, "*_ring_flush_all_caches failed: %d!\n", ret);
  2391. }
  2392. trace_i915_gem_request_add(request);
  2393. request->head = request_start;
  2394. /* Whilst this request exists, batch_obj will be on the
  2395. * active_list, and so will hold the active reference. Only when this
  2396. * request is retired will the the batch_obj be moved onto the
  2397. * inactive_list and lose its active reference. Hence we do not need
  2398. * to explicitly hold another reference here.
  2399. */
  2400. request->batch_obj = obj;
  2401. /* Seal the request and mark it as pending execution. Note that
  2402. * we may inspect this state, without holding any locks, during
  2403. * hangcheck. Hence we apply the barrier to ensure that we do not
  2404. * see a more recent value in the hws than we are tracking.
  2405. */
  2406. request->emitted_jiffies = jiffies;
  2407. request->previous_seqno = engine->last_submitted_seqno;
  2408. smp_store_mb(engine->last_submitted_seqno, request->seqno);
  2409. list_add_tail(&request->list, &engine->request_list);
  2410. /* Record the position of the start of the request so that
  2411. * should we detect the updated seqno part-way through the
  2412. * GPU processing the request, we never over-estimate the
  2413. * position of the head.
  2414. */
  2415. request->postfix = intel_ring_get_tail(ringbuf);
  2416. if (i915.enable_execlists)
  2417. ret = engine->emit_request(request);
  2418. else {
  2419. ret = engine->add_request(request);
  2420. request->tail = intel_ring_get_tail(ringbuf);
  2421. }
  2422. /* Not allowed to fail! */
  2423. WARN(ret, "emit|add_request failed: %d!\n", ret);
  2424. queue_delayed_work(dev_priv->wq,
  2425. &dev_priv->mm.retire_work,
  2426. round_jiffies_up_relative(HZ));
  2427. intel_mark_busy(dev_priv);
  2428. /* Sanity check that the reserved size was large enough. */
  2429. ret = intel_ring_get_tail(ringbuf) - request_start;
  2430. if (ret < 0)
  2431. ret += ringbuf->size;
  2432. WARN_ONCE(ret > reserved_tail,
  2433. "Not enough space reserved (%d bytes) "
  2434. "for adding the request (%d bytes)\n",
  2435. reserved_tail, ret);
  2436. }
  2437. static bool i915_context_is_banned(struct drm_i915_private *dev_priv,
  2438. const struct i915_gem_context *ctx)
  2439. {
  2440. unsigned long elapsed;
  2441. elapsed = get_seconds() - ctx->hang_stats.guilty_ts;
  2442. if (ctx->hang_stats.banned)
  2443. return true;
  2444. if (ctx->hang_stats.ban_period_seconds &&
  2445. elapsed <= ctx->hang_stats.ban_period_seconds) {
  2446. if (!i915_gem_context_is_default(ctx)) {
  2447. DRM_DEBUG("context hanging too fast, banning!\n");
  2448. return true;
  2449. } else if (i915_stop_ring_allow_ban(dev_priv)) {
  2450. if (i915_stop_ring_allow_warn(dev_priv))
  2451. DRM_ERROR("gpu hanging too fast, banning!\n");
  2452. return true;
  2453. }
  2454. }
  2455. return false;
  2456. }
  2457. static void i915_set_reset_status(struct drm_i915_private *dev_priv,
  2458. struct i915_gem_context *ctx,
  2459. const bool guilty)
  2460. {
  2461. struct i915_ctx_hang_stats *hs;
  2462. if (WARN_ON(!ctx))
  2463. return;
  2464. hs = &ctx->hang_stats;
  2465. if (guilty) {
  2466. hs->banned = i915_context_is_banned(dev_priv, ctx);
  2467. hs->batch_active++;
  2468. hs->guilty_ts = get_seconds();
  2469. } else {
  2470. hs->batch_pending++;
  2471. }
  2472. }
  2473. void i915_gem_request_free(struct kref *req_ref)
  2474. {
  2475. struct drm_i915_gem_request *req = container_of(req_ref,
  2476. typeof(*req), ref);
  2477. kmem_cache_free(req->i915->requests, req);
  2478. }
  2479. static inline int
  2480. __i915_gem_request_alloc(struct intel_engine_cs *engine,
  2481. struct i915_gem_context *ctx,
  2482. struct drm_i915_gem_request **req_out)
  2483. {
  2484. struct drm_i915_private *dev_priv = engine->i915;
  2485. unsigned reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  2486. struct drm_i915_gem_request *req;
  2487. int ret;
  2488. if (!req_out)
  2489. return -EINVAL;
  2490. *req_out = NULL;
  2491. /* ABI: Before userspace accesses the GPU (e.g. execbuffer), report
  2492. * EIO if the GPU is already wedged, or EAGAIN to drop the struct_mutex
  2493. * and restart.
  2494. */
  2495. ret = i915_gem_check_wedge(reset_counter, dev_priv->mm.interruptible);
  2496. if (ret)
  2497. return ret;
  2498. req = kmem_cache_zalloc(dev_priv->requests, GFP_KERNEL);
  2499. if (req == NULL)
  2500. return -ENOMEM;
  2501. ret = i915_gem_get_seqno(engine->i915, &req->seqno);
  2502. if (ret)
  2503. goto err;
  2504. kref_init(&req->ref);
  2505. req->i915 = dev_priv;
  2506. req->engine = engine;
  2507. req->ctx = ctx;
  2508. i915_gem_context_reference(req->ctx);
  2509. /*
  2510. * Reserve space in the ring buffer for all the commands required to
  2511. * eventually emit this request. This is to guarantee that the
  2512. * i915_add_request() call can't fail. Note that the reserve may need
  2513. * to be redone if the request is not actually submitted straight
  2514. * away, e.g. because a GPU scheduler has deferred it.
  2515. */
  2516. req->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
  2517. if (i915.enable_execlists)
  2518. ret = intel_logical_ring_alloc_request_extras(req);
  2519. else
  2520. ret = intel_ring_alloc_request_extras(req);
  2521. if (ret)
  2522. goto err_ctx;
  2523. *req_out = req;
  2524. return 0;
  2525. err_ctx:
  2526. i915_gem_context_unreference(ctx);
  2527. err:
  2528. kmem_cache_free(dev_priv->requests, req);
  2529. return ret;
  2530. }
  2531. /**
  2532. * i915_gem_request_alloc - allocate a request structure
  2533. *
  2534. * @engine: engine that we wish to issue the request on.
  2535. * @ctx: context that the request will be associated with.
  2536. * This can be NULL if the request is not directly related to
  2537. * any specific user context, in which case this function will
  2538. * choose an appropriate context to use.
  2539. *
  2540. * Returns a pointer to the allocated request if successful,
  2541. * or an error code if not.
  2542. */
  2543. struct drm_i915_gem_request *
  2544. i915_gem_request_alloc(struct intel_engine_cs *engine,
  2545. struct i915_gem_context *ctx)
  2546. {
  2547. struct drm_i915_gem_request *req;
  2548. int err;
  2549. if (ctx == NULL)
  2550. ctx = engine->i915->kernel_context;
  2551. err = __i915_gem_request_alloc(engine, ctx, &req);
  2552. return err ? ERR_PTR(err) : req;
  2553. }
  2554. struct drm_i915_gem_request *
  2555. i915_gem_find_active_request(struct intel_engine_cs *engine)
  2556. {
  2557. struct drm_i915_gem_request *request;
  2558. /* We are called by the error capture and reset at a random
  2559. * point in time. In particular, note that neither is crucially
  2560. * ordered with an interrupt. After a hang, the GPU is dead and we
  2561. * assume that no more writes can happen (we waited long enough for
  2562. * all writes that were in transaction to be flushed) - adding an
  2563. * extra delay for a recent interrupt is pointless. Hence, we do
  2564. * not need an engine->irq_seqno_barrier() before the seqno reads.
  2565. */
  2566. list_for_each_entry(request, &engine->request_list, list) {
  2567. if (i915_gem_request_completed(request))
  2568. continue;
  2569. return request;
  2570. }
  2571. return NULL;
  2572. }
  2573. static void i915_gem_reset_engine_status(struct drm_i915_private *dev_priv,
  2574. struct intel_engine_cs *engine)
  2575. {
  2576. struct drm_i915_gem_request *request;
  2577. bool ring_hung;
  2578. request = i915_gem_find_active_request(engine);
  2579. if (request == NULL)
  2580. return;
  2581. ring_hung = engine->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG;
  2582. i915_set_reset_status(dev_priv, request->ctx, ring_hung);
  2583. list_for_each_entry_continue(request, &engine->request_list, list)
  2584. i915_set_reset_status(dev_priv, request->ctx, false);
  2585. }
  2586. static void i915_gem_reset_engine_cleanup(struct drm_i915_private *dev_priv,
  2587. struct intel_engine_cs *engine)
  2588. {
  2589. struct intel_ringbuffer *buffer;
  2590. while (!list_empty(&engine->active_list)) {
  2591. struct drm_i915_gem_object *obj;
  2592. obj = list_first_entry(&engine->active_list,
  2593. struct drm_i915_gem_object,
  2594. engine_list[engine->id]);
  2595. i915_gem_object_retire__read(obj, engine->id);
  2596. }
  2597. /*
  2598. * Clear the execlists queue up before freeing the requests, as those
  2599. * are the ones that keep the context and ringbuffer backing objects
  2600. * pinned in place.
  2601. */
  2602. if (i915.enable_execlists) {
  2603. /* Ensure irq handler finishes or is cancelled. */
  2604. tasklet_kill(&engine->irq_tasklet);
  2605. intel_execlists_cancel_requests(engine);
  2606. }
  2607. /*
  2608. * We must free the requests after all the corresponding objects have
  2609. * been moved off active lists. Which is the same order as the normal
  2610. * retire_requests function does. This is important if object hold
  2611. * implicit references on things like e.g. ppgtt address spaces through
  2612. * the request.
  2613. */
  2614. while (!list_empty(&engine->request_list)) {
  2615. struct drm_i915_gem_request *request;
  2616. request = list_first_entry(&engine->request_list,
  2617. struct drm_i915_gem_request,
  2618. list);
  2619. i915_gem_request_retire(request);
  2620. }
  2621. /* Having flushed all requests from all queues, we know that all
  2622. * ringbuffers must now be empty. However, since we do not reclaim
  2623. * all space when retiring the request (to prevent HEADs colliding
  2624. * with rapid ringbuffer wraparound) the amount of available space
  2625. * upon reset is less than when we start. Do one more pass over
  2626. * all the ringbuffers to reset last_retired_head.
  2627. */
  2628. list_for_each_entry(buffer, &engine->buffers, link) {
  2629. buffer->last_retired_head = buffer->tail;
  2630. intel_ring_update_space(buffer);
  2631. }
  2632. intel_ring_init_seqno(engine, engine->last_submitted_seqno);
  2633. }
  2634. void i915_gem_reset(struct drm_device *dev)
  2635. {
  2636. struct drm_i915_private *dev_priv = dev->dev_private;
  2637. struct intel_engine_cs *engine;
  2638. /*
  2639. * Before we free the objects from the requests, we need to inspect
  2640. * them for finding the guilty party. As the requests only borrow
  2641. * their reference to the objects, the inspection must be done first.
  2642. */
  2643. for_each_engine(engine, dev_priv)
  2644. i915_gem_reset_engine_status(dev_priv, engine);
  2645. for_each_engine(engine, dev_priv)
  2646. i915_gem_reset_engine_cleanup(dev_priv, engine);
  2647. i915_gem_context_reset(dev);
  2648. i915_gem_restore_fences(dev);
  2649. WARN_ON(i915_verify_lists(dev));
  2650. }
  2651. /**
  2652. * This function clears the request list as sequence numbers are passed.
  2653. * @engine: engine to retire requests on
  2654. */
  2655. void
  2656. i915_gem_retire_requests_ring(struct intel_engine_cs *engine)
  2657. {
  2658. WARN_ON(i915_verify_lists(engine->dev));
  2659. /* Retire requests first as we use it above for the early return.
  2660. * If we retire requests last, we may use a later seqno and so clear
  2661. * the requests lists without clearing the active list, leading to
  2662. * confusion.
  2663. */
  2664. while (!list_empty(&engine->request_list)) {
  2665. struct drm_i915_gem_request *request;
  2666. request = list_first_entry(&engine->request_list,
  2667. struct drm_i915_gem_request,
  2668. list);
  2669. if (!i915_gem_request_completed(request))
  2670. break;
  2671. i915_gem_request_retire(request);
  2672. }
  2673. /* Move any buffers on the active list that are no longer referenced
  2674. * by the ringbuffer to the flushing/inactive lists as appropriate,
  2675. * before we free the context associated with the requests.
  2676. */
  2677. while (!list_empty(&engine->active_list)) {
  2678. struct drm_i915_gem_object *obj;
  2679. obj = list_first_entry(&engine->active_list,
  2680. struct drm_i915_gem_object,
  2681. engine_list[engine->id]);
  2682. if (!list_empty(&obj->last_read_req[engine->id]->list))
  2683. break;
  2684. i915_gem_object_retire__read(obj, engine->id);
  2685. }
  2686. if (unlikely(engine->trace_irq_req &&
  2687. i915_gem_request_completed(engine->trace_irq_req))) {
  2688. engine->irq_put(engine);
  2689. i915_gem_request_assign(&engine->trace_irq_req, NULL);
  2690. }
  2691. WARN_ON(i915_verify_lists(engine->dev));
  2692. }
  2693. bool
  2694. i915_gem_retire_requests(struct drm_i915_private *dev_priv)
  2695. {
  2696. struct intel_engine_cs *engine;
  2697. bool idle = true;
  2698. for_each_engine(engine, dev_priv) {
  2699. i915_gem_retire_requests_ring(engine);
  2700. idle &= list_empty(&engine->request_list);
  2701. if (i915.enable_execlists) {
  2702. spin_lock_bh(&engine->execlist_lock);
  2703. idle &= list_empty(&engine->execlist_queue);
  2704. spin_unlock_bh(&engine->execlist_lock);
  2705. }
  2706. }
  2707. if (idle)
  2708. mod_delayed_work(dev_priv->wq,
  2709. &dev_priv->mm.idle_work,
  2710. msecs_to_jiffies(100));
  2711. return idle;
  2712. }
  2713. static void
  2714. i915_gem_retire_work_handler(struct work_struct *work)
  2715. {
  2716. struct drm_i915_private *dev_priv =
  2717. container_of(work, typeof(*dev_priv), mm.retire_work.work);
  2718. struct drm_device *dev = dev_priv->dev;
  2719. bool idle;
  2720. /* Come back later if the device is busy... */
  2721. idle = false;
  2722. if (mutex_trylock(&dev->struct_mutex)) {
  2723. idle = i915_gem_retire_requests(dev_priv);
  2724. mutex_unlock(&dev->struct_mutex);
  2725. }
  2726. if (!idle)
  2727. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  2728. round_jiffies_up_relative(HZ));
  2729. }
  2730. static void
  2731. i915_gem_idle_work_handler(struct work_struct *work)
  2732. {
  2733. struct drm_i915_private *dev_priv =
  2734. container_of(work, typeof(*dev_priv), mm.idle_work.work);
  2735. struct drm_device *dev = dev_priv->dev;
  2736. struct intel_engine_cs *engine;
  2737. for_each_engine(engine, dev_priv)
  2738. if (!list_empty(&engine->request_list))
  2739. return;
  2740. /* we probably should sync with hangcheck here, using cancel_work_sync.
  2741. * Also locking seems to be fubar here, engine->request_list is protected
  2742. * by dev->struct_mutex. */
  2743. intel_mark_idle(dev_priv);
  2744. if (mutex_trylock(&dev->struct_mutex)) {
  2745. for_each_engine(engine, dev_priv)
  2746. i915_gem_batch_pool_fini(&engine->batch_pool);
  2747. mutex_unlock(&dev->struct_mutex);
  2748. }
  2749. }
  2750. /**
  2751. * Ensures that an object will eventually get non-busy by flushing any required
  2752. * write domains, emitting any outstanding lazy request and retiring and
  2753. * completed requests.
  2754. * @obj: object to flush
  2755. */
  2756. static int
  2757. i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
  2758. {
  2759. int i;
  2760. if (!obj->active)
  2761. return 0;
  2762. for (i = 0; i < I915_NUM_ENGINES; i++) {
  2763. struct drm_i915_gem_request *req;
  2764. req = obj->last_read_req[i];
  2765. if (req == NULL)
  2766. continue;
  2767. if (i915_gem_request_completed(req))
  2768. i915_gem_object_retire__read(obj, i);
  2769. }
  2770. return 0;
  2771. }
  2772. /**
  2773. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  2774. * @dev: drm device pointer
  2775. * @data: ioctl data blob
  2776. * @file: drm file pointer
  2777. *
  2778. * Returns 0 if successful, else an error is returned with the remaining time in
  2779. * the timeout parameter.
  2780. * -ETIME: object is still busy after timeout
  2781. * -ERESTARTSYS: signal interrupted the wait
  2782. * -ENONENT: object doesn't exist
  2783. * Also possible, but rare:
  2784. * -EAGAIN: GPU wedged
  2785. * -ENOMEM: damn
  2786. * -ENODEV: Internal IRQ fail
  2787. * -E?: The add request failed
  2788. *
  2789. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  2790. * non-zero timeout parameter the wait ioctl will wait for the given number of
  2791. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  2792. * without holding struct_mutex the object may become re-busied before this
  2793. * function completes. A similar but shorter * race condition exists in the busy
  2794. * ioctl
  2795. */
  2796. int
  2797. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  2798. {
  2799. struct drm_i915_gem_wait *args = data;
  2800. struct drm_i915_gem_object *obj;
  2801. struct drm_i915_gem_request *req[I915_NUM_ENGINES];
  2802. int i, n = 0;
  2803. int ret;
  2804. if (args->flags != 0)
  2805. return -EINVAL;
  2806. ret = i915_mutex_lock_interruptible(dev);
  2807. if (ret)
  2808. return ret;
  2809. obj = to_intel_bo(drm_gem_object_lookup(file, args->bo_handle));
  2810. if (&obj->base == NULL) {
  2811. mutex_unlock(&dev->struct_mutex);
  2812. return -ENOENT;
  2813. }
  2814. /* Need to make sure the object gets inactive eventually. */
  2815. ret = i915_gem_object_flush_active(obj);
  2816. if (ret)
  2817. goto out;
  2818. if (!obj->active)
  2819. goto out;
  2820. /* Do this after OLR check to make sure we make forward progress polling
  2821. * on this IOCTL with a timeout == 0 (like busy ioctl)
  2822. */
  2823. if (args->timeout_ns == 0) {
  2824. ret = -ETIME;
  2825. goto out;
  2826. }
  2827. drm_gem_object_unreference(&obj->base);
  2828. for (i = 0; i < I915_NUM_ENGINES; i++) {
  2829. if (obj->last_read_req[i] == NULL)
  2830. continue;
  2831. req[n++] = i915_gem_request_reference(obj->last_read_req[i]);
  2832. }
  2833. mutex_unlock(&dev->struct_mutex);
  2834. for (i = 0; i < n; i++) {
  2835. if (ret == 0)
  2836. ret = __i915_wait_request(req[i], true,
  2837. args->timeout_ns > 0 ? &args->timeout_ns : NULL,
  2838. to_rps_client(file));
  2839. i915_gem_request_unreference(req[i]);
  2840. }
  2841. return ret;
  2842. out:
  2843. drm_gem_object_unreference(&obj->base);
  2844. mutex_unlock(&dev->struct_mutex);
  2845. return ret;
  2846. }
  2847. static int
  2848. __i915_gem_object_sync(struct drm_i915_gem_object *obj,
  2849. struct intel_engine_cs *to,
  2850. struct drm_i915_gem_request *from_req,
  2851. struct drm_i915_gem_request **to_req)
  2852. {
  2853. struct intel_engine_cs *from;
  2854. int ret;
  2855. from = i915_gem_request_get_engine(from_req);
  2856. if (to == from)
  2857. return 0;
  2858. if (i915_gem_request_completed(from_req))
  2859. return 0;
  2860. if (!i915_semaphore_is_enabled(to_i915(obj->base.dev))) {
  2861. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  2862. ret = __i915_wait_request(from_req,
  2863. i915->mm.interruptible,
  2864. NULL,
  2865. &i915->rps.semaphores);
  2866. if (ret)
  2867. return ret;
  2868. i915_gem_object_retire_request(obj, from_req);
  2869. } else {
  2870. int idx = intel_ring_sync_index(from, to);
  2871. u32 seqno = i915_gem_request_get_seqno(from_req);
  2872. WARN_ON(!to_req);
  2873. if (seqno <= from->semaphore.sync_seqno[idx])
  2874. return 0;
  2875. if (*to_req == NULL) {
  2876. struct drm_i915_gem_request *req;
  2877. req = i915_gem_request_alloc(to, NULL);
  2878. if (IS_ERR(req))
  2879. return PTR_ERR(req);
  2880. *to_req = req;
  2881. }
  2882. trace_i915_gem_ring_sync_to(*to_req, from, from_req);
  2883. ret = to->semaphore.sync_to(*to_req, from, seqno);
  2884. if (ret)
  2885. return ret;
  2886. /* We use last_read_req because sync_to()
  2887. * might have just caused seqno wrap under
  2888. * the radar.
  2889. */
  2890. from->semaphore.sync_seqno[idx] =
  2891. i915_gem_request_get_seqno(obj->last_read_req[from->id]);
  2892. }
  2893. return 0;
  2894. }
  2895. /**
  2896. * i915_gem_object_sync - sync an object to a ring.
  2897. *
  2898. * @obj: object which may be in use on another ring.
  2899. * @to: ring we wish to use the object on. May be NULL.
  2900. * @to_req: request we wish to use the object for. See below.
  2901. * This will be allocated and returned if a request is
  2902. * required but not passed in.
  2903. *
  2904. * This code is meant to abstract object synchronization with the GPU.
  2905. * Calling with NULL implies synchronizing the object with the CPU
  2906. * rather than a particular GPU ring. Conceptually we serialise writes
  2907. * between engines inside the GPU. We only allow one engine to write
  2908. * into a buffer at any time, but multiple readers. To ensure each has
  2909. * a coherent view of memory, we must:
  2910. *
  2911. * - If there is an outstanding write request to the object, the new
  2912. * request must wait for it to complete (either CPU or in hw, requests
  2913. * on the same ring will be naturally ordered).
  2914. *
  2915. * - If we are a write request (pending_write_domain is set), the new
  2916. * request must wait for outstanding read requests to complete.
  2917. *
  2918. * For CPU synchronisation (NULL to) no request is required. For syncing with
  2919. * rings to_req must be non-NULL. However, a request does not have to be
  2920. * pre-allocated. If *to_req is NULL and sync commands will be emitted then a
  2921. * request will be allocated automatically and returned through *to_req. Note
  2922. * that it is not guaranteed that commands will be emitted (because the system
  2923. * might already be idle). Hence there is no need to create a request that
  2924. * might never have any work submitted. Note further that if a request is
  2925. * returned in *to_req, it is the responsibility of the caller to submit
  2926. * that request (after potentially adding more work to it).
  2927. *
  2928. * Returns 0 if successful, else propagates up the lower layer error.
  2929. */
  2930. int
  2931. i915_gem_object_sync(struct drm_i915_gem_object *obj,
  2932. struct intel_engine_cs *to,
  2933. struct drm_i915_gem_request **to_req)
  2934. {
  2935. const bool readonly = obj->base.pending_write_domain == 0;
  2936. struct drm_i915_gem_request *req[I915_NUM_ENGINES];
  2937. int ret, i, n;
  2938. if (!obj->active)
  2939. return 0;
  2940. if (to == NULL)
  2941. return i915_gem_object_wait_rendering(obj, readonly);
  2942. n = 0;
  2943. if (readonly) {
  2944. if (obj->last_write_req)
  2945. req[n++] = obj->last_write_req;
  2946. } else {
  2947. for (i = 0; i < I915_NUM_ENGINES; i++)
  2948. if (obj->last_read_req[i])
  2949. req[n++] = obj->last_read_req[i];
  2950. }
  2951. for (i = 0; i < n; i++) {
  2952. ret = __i915_gem_object_sync(obj, to, req[i], to_req);
  2953. if (ret)
  2954. return ret;
  2955. }
  2956. return 0;
  2957. }
  2958. static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
  2959. {
  2960. u32 old_write_domain, old_read_domains;
  2961. /* Force a pagefault for domain tracking on next user access */
  2962. i915_gem_release_mmap(obj);
  2963. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2964. return;
  2965. old_read_domains = obj->base.read_domains;
  2966. old_write_domain = obj->base.write_domain;
  2967. obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
  2968. obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
  2969. trace_i915_gem_object_change_domain(obj,
  2970. old_read_domains,
  2971. old_write_domain);
  2972. }
  2973. static void __i915_vma_iounmap(struct i915_vma *vma)
  2974. {
  2975. GEM_BUG_ON(vma->pin_count);
  2976. if (vma->iomap == NULL)
  2977. return;
  2978. io_mapping_unmap(vma->iomap);
  2979. vma->iomap = NULL;
  2980. }
  2981. static int __i915_vma_unbind(struct i915_vma *vma, bool wait)
  2982. {
  2983. struct drm_i915_gem_object *obj = vma->obj;
  2984. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2985. int ret;
  2986. if (list_empty(&vma->obj_link))
  2987. return 0;
  2988. if (!drm_mm_node_allocated(&vma->node)) {
  2989. i915_gem_vma_destroy(vma);
  2990. return 0;
  2991. }
  2992. if (vma->pin_count)
  2993. return -EBUSY;
  2994. BUG_ON(obj->pages == NULL);
  2995. if (wait) {
  2996. ret = i915_gem_object_wait_rendering(obj, false);
  2997. if (ret)
  2998. return ret;
  2999. }
  3000. if (vma->is_ggtt && vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
  3001. i915_gem_object_finish_gtt(obj);
  3002. /* release the fence reg _after_ flushing */
  3003. ret = i915_gem_object_put_fence(obj);
  3004. if (ret)
  3005. return ret;
  3006. __i915_vma_iounmap(vma);
  3007. }
  3008. trace_i915_vma_unbind(vma);
  3009. vma->vm->unbind_vma(vma);
  3010. vma->bound = 0;
  3011. list_del_init(&vma->vm_link);
  3012. if (vma->is_ggtt) {
  3013. if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
  3014. obj->map_and_fenceable = false;
  3015. } else if (vma->ggtt_view.pages) {
  3016. sg_free_table(vma->ggtt_view.pages);
  3017. kfree(vma->ggtt_view.pages);
  3018. }
  3019. vma->ggtt_view.pages = NULL;
  3020. }
  3021. drm_mm_remove_node(&vma->node);
  3022. i915_gem_vma_destroy(vma);
  3023. /* Since the unbound list is global, only move to that list if
  3024. * no more VMAs exist. */
  3025. if (list_empty(&obj->vma_list))
  3026. list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  3027. /* And finally now the object is completely decoupled from this vma,
  3028. * we can drop its hold on the backing storage and allow it to be
  3029. * reaped by the shrinker.
  3030. */
  3031. i915_gem_object_unpin_pages(obj);
  3032. return 0;
  3033. }
  3034. int i915_vma_unbind(struct i915_vma *vma)
  3035. {
  3036. return __i915_vma_unbind(vma, true);
  3037. }
  3038. int __i915_vma_unbind_no_wait(struct i915_vma *vma)
  3039. {
  3040. return __i915_vma_unbind(vma, false);
  3041. }
  3042. int i915_gem_wait_for_idle(struct drm_i915_private *dev_priv)
  3043. {
  3044. struct intel_engine_cs *engine;
  3045. int ret;
  3046. lockdep_assert_held(&dev_priv->dev->struct_mutex);
  3047. for_each_engine(engine, dev_priv) {
  3048. if (engine->last_context == NULL)
  3049. continue;
  3050. ret = intel_engine_idle(engine);
  3051. if (ret)
  3052. return ret;
  3053. }
  3054. WARN_ON(i915_verify_lists(dev));
  3055. return 0;
  3056. }
  3057. static bool i915_gem_valid_gtt_space(struct i915_vma *vma,
  3058. unsigned long cache_level)
  3059. {
  3060. struct drm_mm_node *gtt_space = &vma->node;
  3061. struct drm_mm_node *other;
  3062. /*
  3063. * On some machines we have to be careful when putting differing types
  3064. * of snoopable memory together to avoid the prefetcher crossing memory
  3065. * domains and dying. During vm initialisation, we decide whether or not
  3066. * these constraints apply and set the drm_mm.color_adjust
  3067. * appropriately.
  3068. */
  3069. if (vma->vm->mm.color_adjust == NULL)
  3070. return true;
  3071. if (!drm_mm_node_allocated(gtt_space))
  3072. return true;
  3073. if (list_empty(&gtt_space->node_list))
  3074. return true;
  3075. other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
  3076. if (other->allocated && !other->hole_follows && other->color != cache_level)
  3077. return false;
  3078. other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
  3079. if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
  3080. return false;
  3081. return true;
  3082. }
  3083. /**
  3084. * Finds free space in the GTT aperture and binds the object or a view of it
  3085. * there.
  3086. * @obj: object to bind
  3087. * @vm: address space to bind into
  3088. * @ggtt_view: global gtt view if applicable
  3089. * @alignment: requested alignment
  3090. * @flags: mask of PIN_* flags to use
  3091. */
  3092. static struct i915_vma *
  3093. i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
  3094. struct i915_address_space *vm,
  3095. const struct i915_ggtt_view *ggtt_view,
  3096. unsigned alignment,
  3097. uint64_t flags)
  3098. {
  3099. struct drm_device *dev = obj->base.dev;
  3100. struct drm_i915_private *dev_priv = to_i915(dev);
  3101. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  3102. u32 fence_alignment, unfenced_alignment;
  3103. u32 search_flag, alloc_flag;
  3104. u64 start, end;
  3105. u64 size, fence_size;
  3106. struct i915_vma *vma;
  3107. int ret;
  3108. if (i915_is_ggtt(vm)) {
  3109. u32 view_size;
  3110. if (WARN_ON(!ggtt_view))
  3111. return ERR_PTR(-EINVAL);
  3112. view_size = i915_ggtt_view_size(obj, ggtt_view);
  3113. fence_size = i915_gem_get_gtt_size(dev,
  3114. view_size,
  3115. obj->tiling_mode);
  3116. fence_alignment = i915_gem_get_gtt_alignment(dev,
  3117. view_size,
  3118. obj->tiling_mode,
  3119. true);
  3120. unfenced_alignment = i915_gem_get_gtt_alignment(dev,
  3121. view_size,
  3122. obj->tiling_mode,
  3123. false);
  3124. size = flags & PIN_MAPPABLE ? fence_size : view_size;
  3125. } else {
  3126. fence_size = i915_gem_get_gtt_size(dev,
  3127. obj->base.size,
  3128. obj->tiling_mode);
  3129. fence_alignment = i915_gem_get_gtt_alignment(dev,
  3130. obj->base.size,
  3131. obj->tiling_mode,
  3132. true);
  3133. unfenced_alignment =
  3134. i915_gem_get_gtt_alignment(dev,
  3135. obj->base.size,
  3136. obj->tiling_mode,
  3137. false);
  3138. size = flags & PIN_MAPPABLE ? fence_size : obj->base.size;
  3139. }
  3140. start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
  3141. end = vm->total;
  3142. if (flags & PIN_MAPPABLE)
  3143. end = min_t(u64, end, ggtt->mappable_end);
  3144. if (flags & PIN_ZONE_4G)
  3145. end = min_t(u64, end, (1ULL << 32) - PAGE_SIZE);
  3146. if (alignment == 0)
  3147. alignment = flags & PIN_MAPPABLE ? fence_alignment :
  3148. unfenced_alignment;
  3149. if (flags & PIN_MAPPABLE && alignment & (fence_alignment - 1)) {
  3150. DRM_DEBUG("Invalid object (view type=%u) alignment requested %u\n",
  3151. ggtt_view ? ggtt_view->type : 0,
  3152. alignment);
  3153. return ERR_PTR(-EINVAL);
  3154. }
  3155. /* If binding the object/GGTT view requires more space than the entire
  3156. * aperture has, reject it early before evicting everything in a vain
  3157. * attempt to find space.
  3158. */
  3159. if (size > end) {
  3160. DRM_DEBUG("Attempting to bind an object (view type=%u) larger than the aperture: size=%llu > %s aperture=%llu\n",
  3161. ggtt_view ? ggtt_view->type : 0,
  3162. size,
  3163. flags & PIN_MAPPABLE ? "mappable" : "total",
  3164. end);
  3165. return ERR_PTR(-E2BIG);
  3166. }
  3167. ret = i915_gem_object_get_pages(obj);
  3168. if (ret)
  3169. return ERR_PTR(ret);
  3170. i915_gem_object_pin_pages(obj);
  3171. vma = ggtt_view ? i915_gem_obj_lookup_or_create_ggtt_vma(obj, ggtt_view) :
  3172. i915_gem_obj_lookup_or_create_vma(obj, vm);
  3173. if (IS_ERR(vma))
  3174. goto err_unpin;
  3175. if (flags & PIN_OFFSET_FIXED) {
  3176. uint64_t offset = flags & PIN_OFFSET_MASK;
  3177. if (offset & (alignment - 1) || offset + size > end) {
  3178. ret = -EINVAL;
  3179. goto err_free_vma;
  3180. }
  3181. vma->node.start = offset;
  3182. vma->node.size = size;
  3183. vma->node.color = obj->cache_level;
  3184. ret = drm_mm_reserve_node(&vm->mm, &vma->node);
  3185. if (ret) {
  3186. ret = i915_gem_evict_for_vma(vma);
  3187. if (ret == 0)
  3188. ret = drm_mm_reserve_node(&vm->mm, &vma->node);
  3189. }
  3190. if (ret)
  3191. goto err_free_vma;
  3192. } else {
  3193. if (flags & PIN_HIGH) {
  3194. search_flag = DRM_MM_SEARCH_BELOW;
  3195. alloc_flag = DRM_MM_CREATE_TOP;
  3196. } else {
  3197. search_flag = DRM_MM_SEARCH_DEFAULT;
  3198. alloc_flag = DRM_MM_CREATE_DEFAULT;
  3199. }
  3200. search_free:
  3201. ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node,
  3202. size, alignment,
  3203. obj->cache_level,
  3204. start, end,
  3205. search_flag,
  3206. alloc_flag);
  3207. if (ret) {
  3208. ret = i915_gem_evict_something(dev, vm, size, alignment,
  3209. obj->cache_level,
  3210. start, end,
  3211. flags);
  3212. if (ret == 0)
  3213. goto search_free;
  3214. goto err_free_vma;
  3215. }
  3216. }
  3217. if (WARN_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level))) {
  3218. ret = -EINVAL;
  3219. goto err_remove_node;
  3220. }
  3221. trace_i915_vma_bind(vma, flags);
  3222. ret = i915_vma_bind(vma, obj->cache_level, flags);
  3223. if (ret)
  3224. goto err_remove_node;
  3225. list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
  3226. list_add_tail(&vma->vm_link, &vm->inactive_list);
  3227. return vma;
  3228. err_remove_node:
  3229. drm_mm_remove_node(&vma->node);
  3230. err_free_vma:
  3231. i915_gem_vma_destroy(vma);
  3232. vma = ERR_PTR(ret);
  3233. err_unpin:
  3234. i915_gem_object_unpin_pages(obj);
  3235. return vma;
  3236. }
  3237. bool
  3238. i915_gem_clflush_object(struct drm_i915_gem_object *obj,
  3239. bool force)
  3240. {
  3241. /* If we don't have a page list set up, then we're not pinned
  3242. * to GPU, and we can ignore the cache flush because it'll happen
  3243. * again at bind time.
  3244. */
  3245. if (obj->pages == NULL)
  3246. return false;
  3247. /*
  3248. * Stolen memory is always coherent with the GPU as it is explicitly
  3249. * marked as wc by the system, or the system is cache-coherent.
  3250. */
  3251. if (obj->stolen || obj->phys_handle)
  3252. return false;
  3253. /* If the GPU is snooping the contents of the CPU cache,
  3254. * we do not need to manually clear the CPU cache lines. However,
  3255. * the caches are only snooped when the render cache is
  3256. * flushed/invalidated. As we always have to emit invalidations
  3257. * and flushes when moving into and out of the RENDER domain, correct
  3258. * snooping behaviour occurs naturally as the result of our domain
  3259. * tracking.
  3260. */
  3261. if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) {
  3262. obj->cache_dirty = true;
  3263. return false;
  3264. }
  3265. trace_i915_gem_object_clflush(obj);
  3266. drm_clflush_sg(obj->pages);
  3267. obj->cache_dirty = false;
  3268. return true;
  3269. }
  3270. /** Flushes the GTT write domain for the object if it's dirty. */
  3271. static void
  3272. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  3273. {
  3274. uint32_t old_write_domain;
  3275. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  3276. return;
  3277. /* No actual flushing is required for the GTT write domain. Writes
  3278. * to it immediately go to main memory as far as we know, so there's
  3279. * no chipset flush. It also doesn't land in render cache.
  3280. *
  3281. * However, we do have to enforce the order so that all writes through
  3282. * the GTT land before any writes to the device, such as updates to
  3283. * the GATT itself.
  3284. */
  3285. wmb();
  3286. old_write_domain = obj->base.write_domain;
  3287. obj->base.write_domain = 0;
  3288. intel_fb_obj_flush(obj, false, ORIGIN_GTT);
  3289. trace_i915_gem_object_change_domain(obj,
  3290. obj->base.read_domains,
  3291. old_write_domain);
  3292. }
  3293. /** Flushes the CPU write domain for the object if it's dirty. */
  3294. static void
  3295. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
  3296. {
  3297. uint32_t old_write_domain;
  3298. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  3299. return;
  3300. if (i915_gem_clflush_object(obj, obj->pin_display))
  3301. i915_gem_chipset_flush(to_i915(obj->base.dev));
  3302. old_write_domain = obj->base.write_domain;
  3303. obj->base.write_domain = 0;
  3304. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  3305. trace_i915_gem_object_change_domain(obj,
  3306. obj->base.read_domains,
  3307. old_write_domain);
  3308. }
  3309. /**
  3310. * Moves a single object to the GTT read, and possibly write domain.
  3311. * @obj: object to act on
  3312. * @write: ask for write access or read only
  3313. *
  3314. * This function returns when the move is complete, including waiting on
  3315. * flushes to occur.
  3316. */
  3317. int
  3318. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  3319. {
  3320. struct drm_device *dev = obj->base.dev;
  3321. struct drm_i915_private *dev_priv = to_i915(dev);
  3322. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  3323. uint32_t old_write_domain, old_read_domains;
  3324. struct i915_vma *vma;
  3325. int ret;
  3326. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  3327. return 0;
  3328. ret = i915_gem_object_wait_rendering(obj, !write);
  3329. if (ret)
  3330. return ret;
  3331. /* Flush and acquire obj->pages so that we are coherent through
  3332. * direct access in memory with previous cached writes through
  3333. * shmemfs and that our cache domain tracking remains valid.
  3334. * For example, if the obj->filp was moved to swap without us
  3335. * being notified and releasing the pages, we would mistakenly
  3336. * continue to assume that the obj remained out of the CPU cached
  3337. * domain.
  3338. */
  3339. ret = i915_gem_object_get_pages(obj);
  3340. if (ret)
  3341. return ret;
  3342. i915_gem_object_flush_cpu_write_domain(obj);
  3343. /* Serialise direct access to this object with the barriers for
  3344. * coherent writes from the GPU, by effectively invalidating the
  3345. * GTT domain upon first access.
  3346. */
  3347. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  3348. mb();
  3349. old_write_domain = obj->base.write_domain;
  3350. old_read_domains = obj->base.read_domains;
  3351. /* It should now be out of any other write domains, and we can update
  3352. * the domain values for our changes.
  3353. */
  3354. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  3355. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3356. if (write) {
  3357. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  3358. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  3359. obj->dirty = 1;
  3360. }
  3361. trace_i915_gem_object_change_domain(obj,
  3362. old_read_domains,
  3363. old_write_domain);
  3364. /* And bump the LRU for this access */
  3365. vma = i915_gem_obj_to_ggtt(obj);
  3366. if (vma && drm_mm_node_allocated(&vma->node) && !obj->active)
  3367. list_move_tail(&vma->vm_link,
  3368. &ggtt->base.inactive_list);
  3369. return 0;
  3370. }
  3371. /**
  3372. * Changes the cache-level of an object across all VMA.
  3373. * @obj: object to act on
  3374. * @cache_level: new cache level to set for the object
  3375. *
  3376. * After this function returns, the object will be in the new cache-level
  3377. * across all GTT and the contents of the backing storage will be coherent,
  3378. * with respect to the new cache-level. In order to keep the backing storage
  3379. * coherent for all users, we only allow a single cache level to be set
  3380. * globally on the object and prevent it from being changed whilst the
  3381. * hardware is reading from the object. That is if the object is currently
  3382. * on the scanout it will be set to uncached (or equivalent display
  3383. * cache coherency) and all non-MOCS GPU access will also be uncached so
  3384. * that all direct access to the scanout remains coherent.
  3385. */
  3386. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  3387. enum i915_cache_level cache_level)
  3388. {
  3389. struct drm_device *dev = obj->base.dev;
  3390. struct i915_vma *vma, *next;
  3391. bool bound = false;
  3392. int ret = 0;
  3393. if (obj->cache_level == cache_level)
  3394. goto out;
  3395. /* Inspect the list of currently bound VMA and unbind any that would
  3396. * be invalid given the new cache-level. This is principally to
  3397. * catch the issue of the CS prefetch crossing page boundaries and
  3398. * reading an invalid PTE on older architectures.
  3399. */
  3400. list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link) {
  3401. if (!drm_mm_node_allocated(&vma->node))
  3402. continue;
  3403. if (vma->pin_count) {
  3404. DRM_DEBUG("can not change the cache level of pinned objects\n");
  3405. return -EBUSY;
  3406. }
  3407. if (!i915_gem_valid_gtt_space(vma, cache_level)) {
  3408. ret = i915_vma_unbind(vma);
  3409. if (ret)
  3410. return ret;
  3411. } else
  3412. bound = true;
  3413. }
  3414. /* We can reuse the existing drm_mm nodes but need to change the
  3415. * cache-level on the PTE. We could simply unbind them all and
  3416. * rebind with the correct cache-level on next use. However since
  3417. * we already have a valid slot, dma mapping, pages etc, we may as
  3418. * rewrite the PTE in the belief that doing so tramples upon less
  3419. * state and so involves less work.
  3420. */
  3421. if (bound) {
  3422. /* Before we change the PTE, the GPU must not be accessing it.
  3423. * If we wait upon the object, we know that all the bound
  3424. * VMA are no longer active.
  3425. */
  3426. ret = i915_gem_object_wait_rendering(obj, false);
  3427. if (ret)
  3428. return ret;
  3429. if (!HAS_LLC(dev) && cache_level != I915_CACHE_NONE) {
  3430. /* Access to snoopable pages through the GTT is
  3431. * incoherent and on some machines causes a hard
  3432. * lockup. Relinquish the CPU mmaping to force
  3433. * userspace to refault in the pages and we can
  3434. * then double check if the GTT mapping is still
  3435. * valid for that pointer access.
  3436. */
  3437. i915_gem_release_mmap(obj);
  3438. /* As we no longer need a fence for GTT access,
  3439. * we can relinquish it now (and so prevent having
  3440. * to steal a fence from someone else on the next
  3441. * fence request). Note GPU activity would have
  3442. * dropped the fence as all snoopable access is
  3443. * supposed to be linear.
  3444. */
  3445. ret = i915_gem_object_put_fence(obj);
  3446. if (ret)
  3447. return ret;
  3448. } else {
  3449. /* We either have incoherent backing store and
  3450. * so no GTT access or the architecture is fully
  3451. * coherent. In such cases, existing GTT mmaps
  3452. * ignore the cache bit in the PTE and we can
  3453. * rewrite it without confusing the GPU or having
  3454. * to force userspace to fault back in its mmaps.
  3455. */
  3456. }
  3457. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  3458. if (!drm_mm_node_allocated(&vma->node))
  3459. continue;
  3460. ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
  3461. if (ret)
  3462. return ret;
  3463. }
  3464. }
  3465. list_for_each_entry(vma, &obj->vma_list, obj_link)
  3466. vma->node.color = cache_level;
  3467. obj->cache_level = cache_level;
  3468. out:
  3469. /* Flush the dirty CPU caches to the backing storage so that the
  3470. * object is now coherent at its new cache level (with respect
  3471. * to the access domain).
  3472. */
  3473. if (obj->cache_dirty && cpu_write_needs_clflush(obj)) {
  3474. if (i915_gem_clflush_object(obj, true))
  3475. i915_gem_chipset_flush(to_i915(obj->base.dev));
  3476. }
  3477. return 0;
  3478. }
  3479. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  3480. struct drm_file *file)
  3481. {
  3482. struct drm_i915_gem_caching *args = data;
  3483. struct drm_i915_gem_object *obj;
  3484. obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
  3485. if (&obj->base == NULL)
  3486. return -ENOENT;
  3487. switch (obj->cache_level) {
  3488. case I915_CACHE_LLC:
  3489. case I915_CACHE_L3_LLC:
  3490. args->caching = I915_CACHING_CACHED;
  3491. break;
  3492. case I915_CACHE_WT:
  3493. args->caching = I915_CACHING_DISPLAY;
  3494. break;
  3495. default:
  3496. args->caching = I915_CACHING_NONE;
  3497. break;
  3498. }
  3499. drm_gem_object_unreference_unlocked(&obj->base);
  3500. return 0;
  3501. }
  3502. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  3503. struct drm_file *file)
  3504. {
  3505. struct drm_i915_private *dev_priv = dev->dev_private;
  3506. struct drm_i915_gem_caching *args = data;
  3507. struct drm_i915_gem_object *obj;
  3508. enum i915_cache_level level;
  3509. int ret;
  3510. switch (args->caching) {
  3511. case I915_CACHING_NONE:
  3512. level = I915_CACHE_NONE;
  3513. break;
  3514. case I915_CACHING_CACHED:
  3515. /*
  3516. * Due to a HW issue on BXT A stepping, GPU stores via a
  3517. * snooped mapping may leave stale data in a corresponding CPU
  3518. * cacheline, whereas normally such cachelines would get
  3519. * invalidated.
  3520. */
  3521. if (!HAS_LLC(dev) && !HAS_SNOOP(dev))
  3522. return -ENODEV;
  3523. level = I915_CACHE_LLC;
  3524. break;
  3525. case I915_CACHING_DISPLAY:
  3526. level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE;
  3527. break;
  3528. default:
  3529. return -EINVAL;
  3530. }
  3531. intel_runtime_pm_get(dev_priv);
  3532. ret = i915_mutex_lock_interruptible(dev);
  3533. if (ret)
  3534. goto rpm_put;
  3535. obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
  3536. if (&obj->base == NULL) {
  3537. ret = -ENOENT;
  3538. goto unlock;
  3539. }
  3540. ret = i915_gem_object_set_cache_level(obj, level);
  3541. drm_gem_object_unreference(&obj->base);
  3542. unlock:
  3543. mutex_unlock(&dev->struct_mutex);
  3544. rpm_put:
  3545. intel_runtime_pm_put(dev_priv);
  3546. return ret;
  3547. }
  3548. /*
  3549. * Prepare buffer for display plane (scanout, cursors, etc).
  3550. * Can be called from an uninterruptible phase (modesetting) and allows
  3551. * any flushes to be pipelined (for pageflips).
  3552. */
  3553. int
  3554. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  3555. u32 alignment,
  3556. const struct i915_ggtt_view *view)
  3557. {
  3558. u32 old_read_domains, old_write_domain;
  3559. int ret;
  3560. /* Mark the pin_display early so that we account for the
  3561. * display coherency whilst setting up the cache domains.
  3562. */
  3563. obj->pin_display++;
  3564. /* The display engine is not coherent with the LLC cache on gen6. As
  3565. * a result, we make sure that the pinning that is about to occur is
  3566. * done with uncached PTEs. This is lowest common denominator for all
  3567. * chipsets.
  3568. *
  3569. * However for gen6+, we could do better by using the GFDT bit instead
  3570. * of uncaching, which would allow us to flush all the LLC-cached data
  3571. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  3572. */
  3573. ret = i915_gem_object_set_cache_level(obj,
  3574. HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE);
  3575. if (ret)
  3576. goto err_unpin_display;
  3577. /* As the user may map the buffer once pinned in the display plane
  3578. * (e.g. libkms for the bootup splash), we have to ensure that we
  3579. * always use map_and_fenceable for all scanout buffers.
  3580. */
  3581. ret = i915_gem_object_ggtt_pin(obj, view, alignment,
  3582. view->type == I915_GGTT_VIEW_NORMAL ?
  3583. PIN_MAPPABLE : 0);
  3584. if (ret)
  3585. goto err_unpin_display;
  3586. i915_gem_object_flush_cpu_write_domain(obj);
  3587. old_write_domain = obj->base.write_domain;
  3588. old_read_domains = obj->base.read_domains;
  3589. /* It should now be out of any other write domains, and we can update
  3590. * the domain values for our changes.
  3591. */
  3592. obj->base.write_domain = 0;
  3593. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3594. trace_i915_gem_object_change_domain(obj,
  3595. old_read_domains,
  3596. old_write_domain);
  3597. return 0;
  3598. err_unpin_display:
  3599. obj->pin_display--;
  3600. return ret;
  3601. }
  3602. void
  3603. i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj,
  3604. const struct i915_ggtt_view *view)
  3605. {
  3606. if (WARN_ON(obj->pin_display == 0))
  3607. return;
  3608. i915_gem_object_ggtt_unpin_view(obj, view);
  3609. obj->pin_display--;
  3610. }
  3611. /**
  3612. * Moves a single object to the CPU read, and possibly write domain.
  3613. * @obj: object to act on
  3614. * @write: requesting write or read-only access
  3615. *
  3616. * This function returns when the move is complete, including waiting on
  3617. * flushes to occur.
  3618. */
  3619. int
  3620. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  3621. {
  3622. uint32_t old_write_domain, old_read_domains;
  3623. int ret;
  3624. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  3625. return 0;
  3626. ret = i915_gem_object_wait_rendering(obj, !write);
  3627. if (ret)
  3628. return ret;
  3629. i915_gem_object_flush_gtt_write_domain(obj);
  3630. old_write_domain = obj->base.write_domain;
  3631. old_read_domains = obj->base.read_domains;
  3632. /* Flush the CPU cache if it's still invalid. */
  3633. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  3634. i915_gem_clflush_object(obj, false);
  3635. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  3636. }
  3637. /* It should now be out of any other write domains, and we can update
  3638. * the domain values for our changes.
  3639. */
  3640. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  3641. /* If we're writing through the CPU, then the GPU read domains will
  3642. * need to be invalidated at next use.
  3643. */
  3644. if (write) {
  3645. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3646. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3647. }
  3648. trace_i915_gem_object_change_domain(obj,
  3649. old_read_domains,
  3650. old_write_domain);
  3651. return 0;
  3652. }
  3653. /* Throttle our rendering by waiting until the ring has completed our requests
  3654. * emitted over 20 msec ago.
  3655. *
  3656. * Note that if we were to use the current jiffies each time around the loop,
  3657. * we wouldn't escape the function with any frames outstanding if the time to
  3658. * render a frame was over 20ms.
  3659. *
  3660. * This should get us reasonable parallelism between CPU and GPU but also
  3661. * relatively low latency when blocking on a particular request to finish.
  3662. */
  3663. static int
  3664. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  3665. {
  3666. struct drm_i915_private *dev_priv = dev->dev_private;
  3667. struct drm_i915_file_private *file_priv = file->driver_priv;
  3668. unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
  3669. struct drm_i915_gem_request *request, *target = NULL;
  3670. int ret;
  3671. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  3672. if (ret)
  3673. return ret;
  3674. /* ABI: return -EIO if already wedged */
  3675. if (i915_terminally_wedged(&dev_priv->gpu_error))
  3676. return -EIO;
  3677. spin_lock(&file_priv->mm.lock);
  3678. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  3679. if (time_after_eq(request->emitted_jiffies, recent_enough))
  3680. break;
  3681. /*
  3682. * Note that the request might not have been submitted yet.
  3683. * In which case emitted_jiffies will be zero.
  3684. */
  3685. if (!request->emitted_jiffies)
  3686. continue;
  3687. target = request;
  3688. }
  3689. if (target)
  3690. i915_gem_request_reference(target);
  3691. spin_unlock(&file_priv->mm.lock);
  3692. if (target == NULL)
  3693. return 0;
  3694. ret = __i915_wait_request(target, true, NULL, NULL);
  3695. if (ret == 0)
  3696. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
  3697. i915_gem_request_unreference(target);
  3698. return ret;
  3699. }
  3700. static bool
  3701. i915_vma_misplaced(struct i915_vma *vma, uint32_t alignment, uint64_t flags)
  3702. {
  3703. struct drm_i915_gem_object *obj = vma->obj;
  3704. if (alignment &&
  3705. vma->node.start & (alignment - 1))
  3706. return true;
  3707. if (flags & PIN_MAPPABLE && !obj->map_and_fenceable)
  3708. return true;
  3709. if (flags & PIN_OFFSET_BIAS &&
  3710. vma->node.start < (flags & PIN_OFFSET_MASK))
  3711. return true;
  3712. if (flags & PIN_OFFSET_FIXED &&
  3713. vma->node.start != (flags & PIN_OFFSET_MASK))
  3714. return true;
  3715. return false;
  3716. }
  3717. void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
  3718. {
  3719. struct drm_i915_gem_object *obj = vma->obj;
  3720. bool mappable, fenceable;
  3721. u32 fence_size, fence_alignment;
  3722. fence_size = i915_gem_get_gtt_size(obj->base.dev,
  3723. obj->base.size,
  3724. obj->tiling_mode);
  3725. fence_alignment = i915_gem_get_gtt_alignment(obj->base.dev,
  3726. obj->base.size,
  3727. obj->tiling_mode,
  3728. true);
  3729. fenceable = (vma->node.size == fence_size &&
  3730. (vma->node.start & (fence_alignment - 1)) == 0);
  3731. mappable = (vma->node.start + fence_size <=
  3732. to_i915(obj->base.dev)->ggtt.mappable_end);
  3733. obj->map_and_fenceable = mappable && fenceable;
  3734. }
  3735. static int
  3736. i915_gem_object_do_pin(struct drm_i915_gem_object *obj,
  3737. struct i915_address_space *vm,
  3738. const struct i915_ggtt_view *ggtt_view,
  3739. uint32_t alignment,
  3740. uint64_t flags)
  3741. {
  3742. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3743. struct i915_vma *vma;
  3744. unsigned bound;
  3745. int ret;
  3746. if (WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base))
  3747. return -ENODEV;
  3748. if (WARN_ON(flags & (PIN_GLOBAL | PIN_MAPPABLE) && !i915_is_ggtt(vm)))
  3749. return -EINVAL;
  3750. if (WARN_ON((flags & (PIN_MAPPABLE | PIN_GLOBAL)) == PIN_MAPPABLE))
  3751. return -EINVAL;
  3752. if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
  3753. return -EINVAL;
  3754. vma = ggtt_view ? i915_gem_obj_to_ggtt_view(obj, ggtt_view) :
  3755. i915_gem_obj_to_vma(obj, vm);
  3756. if (vma) {
  3757. if (WARN_ON(vma->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
  3758. return -EBUSY;
  3759. if (i915_vma_misplaced(vma, alignment, flags)) {
  3760. WARN(vma->pin_count,
  3761. "bo is already pinned in %s with incorrect alignment:"
  3762. " offset=%08x %08x, req.alignment=%x, req.map_and_fenceable=%d,"
  3763. " obj->map_and_fenceable=%d\n",
  3764. ggtt_view ? "ggtt" : "ppgtt",
  3765. upper_32_bits(vma->node.start),
  3766. lower_32_bits(vma->node.start),
  3767. alignment,
  3768. !!(flags & PIN_MAPPABLE),
  3769. obj->map_and_fenceable);
  3770. ret = i915_vma_unbind(vma);
  3771. if (ret)
  3772. return ret;
  3773. vma = NULL;
  3774. }
  3775. }
  3776. bound = vma ? vma->bound : 0;
  3777. if (vma == NULL || !drm_mm_node_allocated(&vma->node)) {
  3778. vma = i915_gem_object_bind_to_vm(obj, vm, ggtt_view, alignment,
  3779. flags);
  3780. if (IS_ERR(vma))
  3781. return PTR_ERR(vma);
  3782. } else {
  3783. ret = i915_vma_bind(vma, obj->cache_level, flags);
  3784. if (ret)
  3785. return ret;
  3786. }
  3787. if (ggtt_view && ggtt_view->type == I915_GGTT_VIEW_NORMAL &&
  3788. (bound ^ vma->bound) & GLOBAL_BIND) {
  3789. __i915_vma_set_map_and_fenceable(vma);
  3790. WARN_ON(flags & PIN_MAPPABLE && !obj->map_and_fenceable);
  3791. }
  3792. vma->pin_count++;
  3793. return 0;
  3794. }
  3795. int
  3796. i915_gem_object_pin(struct drm_i915_gem_object *obj,
  3797. struct i915_address_space *vm,
  3798. uint32_t alignment,
  3799. uint64_t flags)
  3800. {
  3801. return i915_gem_object_do_pin(obj, vm,
  3802. i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL,
  3803. alignment, flags);
  3804. }
  3805. int
  3806. i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
  3807. const struct i915_ggtt_view *view,
  3808. uint32_t alignment,
  3809. uint64_t flags)
  3810. {
  3811. struct drm_device *dev = obj->base.dev;
  3812. struct drm_i915_private *dev_priv = to_i915(dev);
  3813. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  3814. BUG_ON(!view);
  3815. return i915_gem_object_do_pin(obj, &ggtt->base, view,
  3816. alignment, flags | PIN_GLOBAL);
  3817. }
  3818. void
  3819. i915_gem_object_ggtt_unpin_view(struct drm_i915_gem_object *obj,
  3820. const struct i915_ggtt_view *view)
  3821. {
  3822. struct i915_vma *vma = i915_gem_obj_to_ggtt_view(obj, view);
  3823. WARN_ON(vma->pin_count == 0);
  3824. WARN_ON(!i915_gem_obj_ggtt_bound_view(obj, view));
  3825. --vma->pin_count;
  3826. }
  3827. int
  3828. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3829. struct drm_file *file)
  3830. {
  3831. struct drm_i915_gem_busy *args = data;
  3832. struct drm_i915_gem_object *obj;
  3833. int ret;
  3834. ret = i915_mutex_lock_interruptible(dev);
  3835. if (ret)
  3836. return ret;
  3837. obj = to_intel_bo(drm_gem_object_lookup(file, args->handle));
  3838. if (&obj->base == NULL) {
  3839. ret = -ENOENT;
  3840. goto unlock;
  3841. }
  3842. /* Count all active objects as busy, even if they are currently not used
  3843. * by the gpu. Users of this interface expect objects to eventually
  3844. * become non-busy without any further actions, therefore emit any
  3845. * necessary flushes here.
  3846. */
  3847. ret = i915_gem_object_flush_active(obj);
  3848. if (ret)
  3849. goto unref;
  3850. args->busy = 0;
  3851. if (obj->active) {
  3852. int i;
  3853. for (i = 0; i < I915_NUM_ENGINES; i++) {
  3854. struct drm_i915_gem_request *req;
  3855. req = obj->last_read_req[i];
  3856. if (req)
  3857. args->busy |= 1 << (16 + req->engine->exec_id);
  3858. }
  3859. if (obj->last_write_req)
  3860. args->busy |= obj->last_write_req->engine->exec_id;
  3861. }
  3862. unref:
  3863. drm_gem_object_unreference(&obj->base);
  3864. unlock:
  3865. mutex_unlock(&dev->struct_mutex);
  3866. return ret;
  3867. }
  3868. int
  3869. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3870. struct drm_file *file_priv)
  3871. {
  3872. return i915_gem_ring_throttle(dev, file_priv);
  3873. }
  3874. int
  3875. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3876. struct drm_file *file_priv)
  3877. {
  3878. struct drm_i915_private *dev_priv = dev->dev_private;
  3879. struct drm_i915_gem_madvise *args = data;
  3880. struct drm_i915_gem_object *obj;
  3881. int ret;
  3882. switch (args->madv) {
  3883. case I915_MADV_DONTNEED:
  3884. case I915_MADV_WILLNEED:
  3885. break;
  3886. default:
  3887. return -EINVAL;
  3888. }
  3889. ret = i915_mutex_lock_interruptible(dev);
  3890. if (ret)
  3891. return ret;
  3892. obj = to_intel_bo(drm_gem_object_lookup(file_priv, args->handle));
  3893. if (&obj->base == NULL) {
  3894. ret = -ENOENT;
  3895. goto unlock;
  3896. }
  3897. if (i915_gem_obj_is_pinned(obj)) {
  3898. ret = -EINVAL;
  3899. goto out;
  3900. }
  3901. if (obj->pages &&
  3902. obj->tiling_mode != I915_TILING_NONE &&
  3903. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  3904. if (obj->madv == I915_MADV_WILLNEED)
  3905. i915_gem_object_unpin_pages(obj);
  3906. if (args->madv == I915_MADV_WILLNEED)
  3907. i915_gem_object_pin_pages(obj);
  3908. }
  3909. if (obj->madv != __I915_MADV_PURGED)
  3910. obj->madv = args->madv;
  3911. /* if the object is no longer attached, discard its backing storage */
  3912. if (obj->madv == I915_MADV_DONTNEED && obj->pages == NULL)
  3913. i915_gem_object_truncate(obj);
  3914. args->retained = obj->madv != __I915_MADV_PURGED;
  3915. out:
  3916. drm_gem_object_unreference(&obj->base);
  3917. unlock:
  3918. mutex_unlock(&dev->struct_mutex);
  3919. return ret;
  3920. }
  3921. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3922. const struct drm_i915_gem_object_ops *ops)
  3923. {
  3924. int i;
  3925. INIT_LIST_HEAD(&obj->global_list);
  3926. for (i = 0; i < I915_NUM_ENGINES; i++)
  3927. INIT_LIST_HEAD(&obj->engine_list[i]);
  3928. INIT_LIST_HEAD(&obj->obj_exec_link);
  3929. INIT_LIST_HEAD(&obj->vma_list);
  3930. INIT_LIST_HEAD(&obj->batch_pool_link);
  3931. obj->ops = ops;
  3932. obj->fence_reg = I915_FENCE_REG_NONE;
  3933. obj->madv = I915_MADV_WILLNEED;
  3934. i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
  3935. }
  3936. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3937. .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE,
  3938. .get_pages = i915_gem_object_get_pages_gtt,
  3939. .put_pages = i915_gem_object_put_pages_gtt,
  3940. };
  3941. struct drm_i915_gem_object *i915_gem_object_create(struct drm_device *dev,
  3942. size_t size)
  3943. {
  3944. struct drm_i915_gem_object *obj;
  3945. struct address_space *mapping;
  3946. gfp_t mask;
  3947. int ret;
  3948. obj = i915_gem_object_alloc(dev);
  3949. if (obj == NULL)
  3950. return ERR_PTR(-ENOMEM);
  3951. ret = drm_gem_object_init(dev, &obj->base, size);
  3952. if (ret)
  3953. goto fail;
  3954. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3955. if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
  3956. /* 965gm cannot relocate objects above 4GiB. */
  3957. mask &= ~__GFP_HIGHMEM;
  3958. mask |= __GFP_DMA32;
  3959. }
  3960. mapping = file_inode(obj->base.filp)->i_mapping;
  3961. mapping_set_gfp_mask(mapping, mask);
  3962. i915_gem_object_init(obj, &i915_gem_object_ops);
  3963. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3964. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3965. if (HAS_LLC(dev)) {
  3966. /* On some devices, we can have the GPU use the LLC (the CPU
  3967. * cache) for about a 10% performance improvement
  3968. * compared to uncached. Graphics requests other than
  3969. * display scanout are coherent with the CPU in
  3970. * accessing this cache. This means in this mode we
  3971. * don't need to clflush on the CPU side, and on the
  3972. * GPU side we only need to flush internal caches to
  3973. * get data visible to the CPU.
  3974. *
  3975. * However, we maintain the display planes as UC, and so
  3976. * need to rebind when first used as such.
  3977. */
  3978. obj->cache_level = I915_CACHE_LLC;
  3979. } else
  3980. obj->cache_level = I915_CACHE_NONE;
  3981. trace_i915_gem_object_create(obj);
  3982. return obj;
  3983. fail:
  3984. i915_gem_object_free(obj);
  3985. return ERR_PTR(ret);
  3986. }
  3987. static bool discard_backing_storage(struct drm_i915_gem_object *obj)
  3988. {
  3989. /* If we are the last user of the backing storage (be it shmemfs
  3990. * pages or stolen etc), we know that the pages are going to be
  3991. * immediately released. In this case, we can then skip copying
  3992. * back the contents from the GPU.
  3993. */
  3994. if (obj->madv != I915_MADV_WILLNEED)
  3995. return false;
  3996. if (obj->base.filp == NULL)
  3997. return true;
  3998. /* At first glance, this looks racy, but then again so would be
  3999. * userspace racing mmap against close. However, the first external
  4000. * reference to the filp can only be obtained through the
  4001. * i915_gem_mmap_ioctl() which safeguards us against the user
  4002. * acquiring such a reference whilst we are in the middle of
  4003. * freeing the object.
  4004. */
  4005. return atomic_long_read(&obj->base.filp->f_count) == 1;
  4006. }
  4007. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  4008. {
  4009. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  4010. struct drm_device *dev = obj->base.dev;
  4011. struct drm_i915_private *dev_priv = dev->dev_private;
  4012. struct i915_vma *vma, *next;
  4013. intel_runtime_pm_get(dev_priv);
  4014. trace_i915_gem_object_destroy(obj);
  4015. list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link) {
  4016. int ret;
  4017. vma->pin_count = 0;
  4018. ret = i915_vma_unbind(vma);
  4019. if (WARN_ON(ret == -ERESTARTSYS)) {
  4020. bool was_interruptible;
  4021. was_interruptible = dev_priv->mm.interruptible;
  4022. dev_priv->mm.interruptible = false;
  4023. WARN_ON(i915_vma_unbind(vma));
  4024. dev_priv->mm.interruptible = was_interruptible;
  4025. }
  4026. }
  4027. /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
  4028. * before progressing. */
  4029. if (obj->stolen)
  4030. i915_gem_object_unpin_pages(obj);
  4031. WARN_ON(obj->frontbuffer_bits);
  4032. if (obj->pages && obj->madv == I915_MADV_WILLNEED &&
  4033. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES &&
  4034. obj->tiling_mode != I915_TILING_NONE)
  4035. i915_gem_object_unpin_pages(obj);
  4036. if (WARN_ON(obj->pages_pin_count))
  4037. obj->pages_pin_count = 0;
  4038. if (discard_backing_storage(obj))
  4039. obj->madv = I915_MADV_DONTNEED;
  4040. i915_gem_object_put_pages(obj);
  4041. i915_gem_object_free_mmap_offset(obj);
  4042. BUG_ON(obj->pages);
  4043. if (obj->base.import_attach)
  4044. drm_prime_gem_destroy(&obj->base, NULL);
  4045. if (obj->ops->release)
  4046. obj->ops->release(obj);
  4047. drm_gem_object_release(&obj->base);
  4048. i915_gem_info_remove_obj(dev_priv, obj->base.size);
  4049. kfree(obj->bit_17);
  4050. i915_gem_object_free(obj);
  4051. intel_runtime_pm_put(dev_priv);
  4052. }
  4053. struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
  4054. struct i915_address_space *vm)
  4055. {
  4056. struct i915_vma *vma;
  4057. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  4058. if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL &&
  4059. vma->vm == vm)
  4060. return vma;
  4061. }
  4062. return NULL;
  4063. }
  4064. struct i915_vma *i915_gem_obj_to_ggtt_view(struct drm_i915_gem_object *obj,
  4065. const struct i915_ggtt_view *view)
  4066. {
  4067. struct i915_vma *vma;
  4068. GEM_BUG_ON(!view);
  4069. list_for_each_entry(vma, &obj->vma_list, obj_link)
  4070. if (vma->is_ggtt && i915_ggtt_view_equal(&vma->ggtt_view, view))
  4071. return vma;
  4072. return NULL;
  4073. }
  4074. void i915_gem_vma_destroy(struct i915_vma *vma)
  4075. {
  4076. WARN_ON(vma->node.allocated);
  4077. /* Keep the vma as a placeholder in the execbuffer reservation lists */
  4078. if (!list_empty(&vma->exec_list))
  4079. return;
  4080. if (!vma->is_ggtt)
  4081. i915_ppgtt_put(i915_vm_to_ppgtt(vma->vm));
  4082. list_del(&vma->obj_link);
  4083. kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
  4084. }
  4085. static void
  4086. i915_gem_stop_engines(struct drm_device *dev)
  4087. {
  4088. struct drm_i915_private *dev_priv = dev->dev_private;
  4089. struct intel_engine_cs *engine;
  4090. for_each_engine(engine, dev_priv)
  4091. dev_priv->gt.stop_engine(engine);
  4092. }
  4093. int
  4094. i915_gem_suspend(struct drm_device *dev)
  4095. {
  4096. struct drm_i915_private *dev_priv = dev->dev_private;
  4097. int ret = 0;
  4098. mutex_lock(&dev->struct_mutex);
  4099. ret = i915_gem_wait_for_idle(dev_priv);
  4100. if (ret)
  4101. goto err;
  4102. i915_gem_retire_requests(dev_priv);
  4103. i915_gem_stop_engines(dev);
  4104. i915_gem_context_lost(dev_priv);
  4105. mutex_unlock(&dev->struct_mutex);
  4106. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  4107. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  4108. flush_delayed_work(&dev_priv->mm.idle_work);
  4109. /* Assert that we sucessfully flushed all the work and
  4110. * reset the GPU back to its idle, low power state.
  4111. */
  4112. WARN_ON(dev_priv->mm.busy);
  4113. return 0;
  4114. err:
  4115. mutex_unlock(&dev->struct_mutex);
  4116. return ret;
  4117. }
  4118. void i915_gem_init_swizzling(struct drm_device *dev)
  4119. {
  4120. struct drm_i915_private *dev_priv = dev->dev_private;
  4121. if (INTEL_INFO(dev)->gen < 5 ||
  4122. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  4123. return;
  4124. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  4125. DISP_TILE_SURFACE_SWIZZLING);
  4126. if (IS_GEN5(dev))
  4127. return;
  4128. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  4129. if (IS_GEN6(dev))
  4130. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  4131. else if (IS_GEN7(dev))
  4132. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  4133. else if (IS_GEN8(dev))
  4134. I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
  4135. else
  4136. BUG();
  4137. }
  4138. static void init_unused_ring(struct drm_device *dev, u32 base)
  4139. {
  4140. struct drm_i915_private *dev_priv = dev->dev_private;
  4141. I915_WRITE(RING_CTL(base), 0);
  4142. I915_WRITE(RING_HEAD(base), 0);
  4143. I915_WRITE(RING_TAIL(base), 0);
  4144. I915_WRITE(RING_START(base), 0);
  4145. }
  4146. static void init_unused_rings(struct drm_device *dev)
  4147. {
  4148. if (IS_I830(dev)) {
  4149. init_unused_ring(dev, PRB1_BASE);
  4150. init_unused_ring(dev, SRB0_BASE);
  4151. init_unused_ring(dev, SRB1_BASE);
  4152. init_unused_ring(dev, SRB2_BASE);
  4153. init_unused_ring(dev, SRB3_BASE);
  4154. } else if (IS_GEN2(dev)) {
  4155. init_unused_ring(dev, SRB0_BASE);
  4156. init_unused_ring(dev, SRB1_BASE);
  4157. } else if (IS_GEN3(dev)) {
  4158. init_unused_ring(dev, PRB1_BASE);
  4159. init_unused_ring(dev, PRB2_BASE);
  4160. }
  4161. }
  4162. int i915_gem_init_engines(struct drm_device *dev)
  4163. {
  4164. struct drm_i915_private *dev_priv = dev->dev_private;
  4165. int ret;
  4166. ret = intel_init_render_ring_buffer(dev);
  4167. if (ret)
  4168. return ret;
  4169. if (HAS_BSD(dev)) {
  4170. ret = intel_init_bsd_ring_buffer(dev);
  4171. if (ret)
  4172. goto cleanup_render_ring;
  4173. }
  4174. if (HAS_BLT(dev)) {
  4175. ret = intel_init_blt_ring_buffer(dev);
  4176. if (ret)
  4177. goto cleanup_bsd_ring;
  4178. }
  4179. if (HAS_VEBOX(dev)) {
  4180. ret = intel_init_vebox_ring_buffer(dev);
  4181. if (ret)
  4182. goto cleanup_blt_ring;
  4183. }
  4184. if (HAS_BSD2(dev)) {
  4185. ret = intel_init_bsd2_ring_buffer(dev);
  4186. if (ret)
  4187. goto cleanup_vebox_ring;
  4188. }
  4189. return 0;
  4190. cleanup_vebox_ring:
  4191. intel_cleanup_engine(&dev_priv->engine[VECS]);
  4192. cleanup_blt_ring:
  4193. intel_cleanup_engine(&dev_priv->engine[BCS]);
  4194. cleanup_bsd_ring:
  4195. intel_cleanup_engine(&dev_priv->engine[VCS]);
  4196. cleanup_render_ring:
  4197. intel_cleanup_engine(&dev_priv->engine[RCS]);
  4198. return ret;
  4199. }
  4200. int
  4201. i915_gem_init_hw(struct drm_device *dev)
  4202. {
  4203. struct drm_i915_private *dev_priv = dev->dev_private;
  4204. struct intel_engine_cs *engine;
  4205. int ret;
  4206. /* Double layer security blanket, see i915_gem_init() */
  4207. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  4208. if (HAS_EDRAM(dev) && INTEL_GEN(dev_priv) < 9)
  4209. I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
  4210. if (IS_HASWELL(dev))
  4211. I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev) ?
  4212. LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
  4213. if (HAS_PCH_NOP(dev)) {
  4214. if (IS_IVYBRIDGE(dev)) {
  4215. u32 temp = I915_READ(GEN7_MSG_CTL);
  4216. temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  4217. I915_WRITE(GEN7_MSG_CTL, temp);
  4218. } else if (INTEL_INFO(dev)->gen >= 7) {
  4219. u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
  4220. temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4221. I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
  4222. }
  4223. }
  4224. i915_gem_init_swizzling(dev);
  4225. /*
  4226. * At least 830 can leave some of the unused rings
  4227. * "active" (ie. head != tail) after resume which
  4228. * will prevent c3 entry. Makes sure all unused rings
  4229. * are totally idle.
  4230. */
  4231. init_unused_rings(dev);
  4232. BUG_ON(!dev_priv->kernel_context);
  4233. ret = i915_ppgtt_init_hw(dev);
  4234. if (ret) {
  4235. DRM_ERROR("PPGTT enable HW failed %d\n", ret);
  4236. goto out;
  4237. }
  4238. /* Need to do basic initialisation of all rings first: */
  4239. for_each_engine(engine, dev_priv) {
  4240. ret = engine->init_hw(engine);
  4241. if (ret)
  4242. goto out;
  4243. }
  4244. intel_mocs_init_l3cc_table(dev);
  4245. /* We can't enable contexts until all firmware is loaded */
  4246. ret = intel_guc_setup(dev);
  4247. if (ret)
  4248. goto out;
  4249. /*
  4250. * Increment the next seqno by 0x100 so we have a visible break
  4251. * on re-initialisation
  4252. */
  4253. ret = i915_gem_set_seqno(dev, dev_priv->next_seqno+0x100);
  4254. out:
  4255. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4256. return ret;
  4257. }
  4258. int i915_gem_init(struct drm_device *dev)
  4259. {
  4260. struct drm_i915_private *dev_priv = dev->dev_private;
  4261. int ret;
  4262. mutex_lock(&dev->struct_mutex);
  4263. if (!i915.enable_execlists) {
  4264. dev_priv->gt.execbuf_submit = i915_gem_ringbuffer_submission;
  4265. dev_priv->gt.init_engines = i915_gem_init_engines;
  4266. dev_priv->gt.cleanup_engine = intel_cleanup_engine;
  4267. dev_priv->gt.stop_engine = intel_stop_engine;
  4268. } else {
  4269. dev_priv->gt.execbuf_submit = intel_execlists_submission;
  4270. dev_priv->gt.init_engines = intel_logical_rings_init;
  4271. dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
  4272. dev_priv->gt.stop_engine = intel_logical_ring_stop;
  4273. }
  4274. /* This is just a security blanket to placate dragons.
  4275. * On some systems, we very sporadically observe that the first TLBs
  4276. * used by the CS may be stale, despite us poking the TLB reset. If
  4277. * we hold the forcewake during initialisation these problems
  4278. * just magically go away.
  4279. */
  4280. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  4281. i915_gem_init_userptr(dev_priv);
  4282. i915_gem_init_ggtt(dev);
  4283. ret = i915_gem_context_init(dev);
  4284. if (ret)
  4285. goto out_unlock;
  4286. ret = dev_priv->gt.init_engines(dev);
  4287. if (ret)
  4288. goto out_unlock;
  4289. ret = i915_gem_init_hw(dev);
  4290. if (ret == -EIO) {
  4291. /* Allow ring initialisation to fail by marking the GPU as
  4292. * wedged. But we only want to do this where the GPU is angry,
  4293. * for all other failure, such as an allocation failure, bail.
  4294. */
  4295. DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
  4296. atomic_or(I915_WEDGED, &dev_priv->gpu_error.reset_counter);
  4297. ret = 0;
  4298. }
  4299. out_unlock:
  4300. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4301. mutex_unlock(&dev->struct_mutex);
  4302. return ret;
  4303. }
  4304. void
  4305. i915_gem_cleanup_engines(struct drm_device *dev)
  4306. {
  4307. struct drm_i915_private *dev_priv = dev->dev_private;
  4308. struct intel_engine_cs *engine;
  4309. for_each_engine(engine, dev_priv)
  4310. dev_priv->gt.cleanup_engine(engine);
  4311. }
  4312. static void
  4313. init_engine_lists(struct intel_engine_cs *engine)
  4314. {
  4315. INIT_LIST_HEAD(&engine->active_list);
  4316. INIT_LIST_HEAD(&engine->request_list);
  4317. }
  4318. void
  4319. i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
  4320. {
  4321. struct drm_device *dev = dev_priv->dev;
  4322. if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
  4323. !IS_CHERRYVIEW(dev_priv))
  4324. dev_priv->num_fence_regs = 32;
  4325. else if (INTEL_INFO(dev_priv)->gen >= 4 || IS_I945G(dev_priv) ||
  4326. IS_I945GM(dev_priv) || IS_G33(dev_priv))
  4327. dev_priv->num_fence_regs = 16;
  4328. else
  4329. dev_priv->num_fence_regs = 8;
  4330. if (intel_vgpu_active(dev_priv))
  4331. dev_priv->num_fence_regs =
  4332. I915_READ(vgtif_reg(avail_rs.fence_num));
  4333. /* Initialize fence registers to zero */
  4334. i915_gem_restore_fences(dev);
  4335. i915_gem_detect_bit_6_swizzle(dev);
  4336. }
  4337. void
  4338. i915_gem_load_init(struct drm_device *dev)
  4339. {
  4340. struct drm_i915_private *dev_priv = dev->dev_private;
  4341. int i;
  4342. dev_priv->objects =
  4343. kmem_cache_create("i915_gem_object",
  4344. sizeof(struct drm_i915_gem_object), 0,
  4345. SLAB_HWCACHE_ALIGN,
  4346. NULL);
  4347. dev_priv->vmas =
  4348. kmem_cache_create("i915_gem_vma",
  4349. sizeof(struct i915_vma), 0,
  4350. SLAB_HWCACHE_ALIGN,
  4351. NULL);
  4352. dev_priv->requests =
  4353. kmem_cache_create("i915_gem_request",
  4354. sizeof(struct drm_i915_gem_request), 0,
  4355. SLAB_HWCACHE_ALIGN,
  4356. NULL);
  4357. INIT_LIST_HEAD(&dev_priv->vm_list);
  4358. INIT_LIST_HEAD(&dev_priv->context_list);
  4359. INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  4360. INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  4361. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4362. for (i = 0; i < I915_NUM_ENGINES; i++)
  4363. init_engine_lists(&dev_priv->engine[i]);
  4364. for (i = 0; i < I915_MAX_NUM_FENCES; i++)
  4365. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  4366. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  4367. i915_gem_retire_work_handler);
  4368. INIT_DELAYED_WORK(&dev_priv->mm.idle_work,
  4369. i915_gem_idle_work_handler);
  4370. init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
  4371. init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  4372. dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  4373. /*
  4374. * Set initial sequence number for requests.
  4375. * Using this number allows the wraparound to happen early,
  4376. * catching any obvious problems.
  4377. */
  4378. dev_priv->next_seqno = ((u32)~0 - 0x1100);
  4379. dev_priv->last_seqno = ((u32)~0 - 0x1101);
  4380. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4381. init_waitqueue_head(&dev_priv->pending_flip_queue);
  4382. dev_priv->mm.interruptible = true;
  4383. mutex_init(&dev_priv->fb_tracking.lock);
  4384. }
  4385. void i915_gem_load_cleanup(struct drm_device *dev)
  4386. {
  4387. struct drm_i915_private *dev_priv = to_i915(dev);
  4388. kmem_cache_destroy(dev_priv->requests);
  4389. kmem_cache_destroy(dev_priv->vmas);
  4390. kmem_cache_destroy(dev_priv->objects);
  4391. }
  4392. int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
  4393. {
  4394. struct drm_i915_gem_object *obj;
  4395. /* Called just before we write the hibernation image.
  4396. *
  4397. * We need to update the domain tracking to reflect that the CPU
  4398. * will be accessing all the pages to create and restore from the
  4399. * hibernation, and so upon restoration those pages will be in the
  4400. * CPU domain.
  4401. *
  4402. * To make sure the hibernation image contains the latest state,
  4403. * we update that state just before writing out the image.
  4404. */
  4405. list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list) {
  4406. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  4407. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  4408. }
  4409. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  4410. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  4411. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  4412. }
  4413. return 0;
  4414. }
  4415. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  4416. {
  4417. struct drm_i915_file_private *file_priv = file->driver_priv;
  4418. /* Clean up our request list when the client is going away, so that
  4419. * later retire_requests won't dereference our soon-to-be-gone
  4420. * file_priv.
  4421. */
  4422. spin_lock(&file_priv->mm.lock);
  4423. while (!list_empty(&file_priv->mm.request_list)) {
  4424. struct drm_i915_gem_request *request;
  4425. request = list_first_entry(&file_priv->mm.request_list,
  4426. struct drm_i915_gem_request,
  4427. client_list);
  4428. list_del(&request->client_list);
  4429. request->file_priv = NULL;
  4430. }
  4431. spin_unlock(&file_priv->mm.lock);
  4432. if (!list_empty(&file_priv->rps.link)) {
  4433. spin_lock(&to_i915(dev)->rps.client_lock);
  4434. list_del(&file_priv->rps.link);
  4435. spin_unlock(&to_i915(dev)->rps.client_lock);
  4436. }
  4437. }
  4438. int i915_gem_open(struct drm_device *dev, struct drm_file *file)
  4439. {
  4440. struct drm_i915_file_private *file_priv;
  4441. int ret;
  4442. DRM_DEBUG_DRIVER("\n");
  4443. file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
  4444. if (!file_priv)
  4445. return -ENOMEM;
  4446. file->driver_priv = file_priv;
  4447. file_priv->dev_priv = dev->dev_private;
  4448. file_priv->file = file;
  4449. INIT_LIST_HEAD(&file_priv->rps.link);
  4450. spin_lock_init(&file_priv->mm.lock);
  4451. INIT_LIST_HEAD(&file_priv->mm.request_list);
  4452. file_priv->bsd_ring = -1;
  4453. ret = i915_gem_context_open(dev, file);
  4454. if (ret)
  4455. kfree(file_priv);
  4456. return ret;
  4457. }
  4458. /**
  4459. * i915_gem_track_fb - update frontbuffer tracking
  4460. * @old: current GEM buffer for the frontbuffer slots
  4461. * @new: new GEM buffer for the frontbuffer slots
  4462. * @frontbuffer_bits: bitmask of frontbuffer slots
  4463. *
  4464. * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
  4465. * from @old and setting them in @new. Both @old and @new can be NULL.
  4466. */
  4467. void i915_gem_track_fb(struct drm_i915_gem_object *old,
  4468. struct drm_i915_gem_object *new,
  4469. unsigned frontbuffer_bits)
  4470. {
  4471. if (old) {
  4472. WARN_ON(!mutex_is_locked(&old->base.dev->struct_mutex));
  4473. WARN_ON(!(old->frontbuffer_bits & frontbuffer_bits));
  4474. old->frontbuffer_bits &= ~frontbuffer_bits;
  4475. }
  4476. if (new) {
  4477. WARN_ON(!mutex_is_locked(&new->base.dev->struct_mutex));
  4478. WARN_ON(new->frontbuffer_bits & frontbuffer_bits);
  4479. new->frontbuffer_bits |= frontbuffer_bits;
  4480. }
  4481. }
  4482. /* All the new VM stuff */
  4483. u64 i915_gem_obj_offset(struct drm_i915_gem_object *o,
  4484. struct i915_address_space *vm)
  4485. {
  4486. struct drm_i915_private *dev_priv = o->base.dev->dev_private;
  4487. struct i915_vma *vma;
  4488. WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
  4489. list_for_each_entry(vma, &o->vma_list, obj_link) {
  4490. if (vma->is_ggtt &&
  4491. vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
  4492. continue;
  4493. if (vma->vm == vm)
  4494. return vma->node.start;
  4495. }
  4496. WARN(1, "%s vma for this object not found.\n",
  4497. i915_is_ggtt(vm) ? "global" : "ppgtt");
  4498. return -1;
  4499. }
  4500. u64 i915_gem_obj_ggtt_offset_view(struct drm_i915_gem_object *o,
  4501. const struct i915_ggtt_view *view)
  4502. {
  4503. struct i915_vma *vma;
  4504. list_for_each_entry(vma, &o->vma_list, obj_link)
  4505. if (vma->is_ggtt && i915_ggtt_view_equal(&vma->ggtt_view, view))
  4506. return vma->node.start;
  4507. WARN(1, "global vma for this object not found. (view=%u)\n", view->type);
  4508. return -1;
  4509. }
  4510. bool i915_gem_obj_bound(struct drm_i915_gem_object *o,
  4511. struct i915_address_space *vm)
  4512. {
  4513. struct i915_vma *vma;
  4514. list_for_each_entry(vma, &o->vma_list, obj_link) {
  4515. if (vma->is_ggtt &&
  4516. vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
  4517. continue;
  4518. if (vma->vm == vm && drm_mm_node_allocated(&vma->node))
  4519. return true;
  4520. }
  4521. return false;
  4522. }
  4523. bool i915_gem_obj_ggtt_bound_view(struct drm_i915_gem_object *o,
  4524. const struct i915_ggtt_view *view)
  4525. {
  4526. struct i915_vma *vma;
  4527. list_for_each_entry(vma, &o->vma_list, obj_link)
  4528. if (vma->is_ggtt &&
  4529. i915_ggtt_view_equal(&vma->ggtt_view, view) &&
  4530. drm_mm_node_allocated(&vma->node))
  4531. return true;
  4532. return false;
  4533. }
  4534. bool i915_gem_obj_bound_any(struct drm_i915_gem_object *o)
  4535. {
  4536. struct i915_vma *vma;
  4537. list_for_each_entry(vma, &o->vma_list, obj_link)
  4538. if (drm_mm_node_allocated(&vma->node))
  4539. return true;
  4540. return false;
  4541. }
  4542. unsigned long i915_gem_obj_ggtt_size(struct drm_i915_gem_object *o)
  4543. {
  4544. struct i915_vma *vma;
  4545. GEM_BUG_ON(list_empty(&o->vma_list));
  4546. list_for_each_entry(vma, &o->vma_list, obj_link) {
  4547. if (vma->is_ggtt &&
  4548. vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
  4549. return vma->node.size;
  4550. }
  4551. return 0;
  4552. }
  4553. bool i915_gem_obj_is_pinned(struct drm_i915_gem_object *obj)
  4554. {
  4555. struct i915_vma *vma;
  4556. list_for_each_entry(vma, &obj->vma_list, obj_link)
  4557. if (vma->pin_count > 0)
  4558. return true;
  4559. return false;
  4560. }
  4561. /* Like i915_gem_object_get_page(), but mark the returned page dirty */
  4562. struct page *
  4563. i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, int n)
  4564. {
  4565. struct page *page;
  4566. /* Only default objects have per-page dirty tracking */
  4567. if (WARN_ON(!i915_gem_object_has_struct_page(obj)))
  4568. return NULL;
  4569. page = i915_gem_object_get_page(obj, n);
  4570. set_page_dirty(page);
  4571. return page;
  4572. }
  4573. /* Allocate a new GEM object and fill it with the supplied data */
  4574. struct drm_i915_gem_object *
  4575. i915_gem_object_create_from_data(struct drm_device *dev,
  4576. const void *data, size_t size)
  4577. {
  4578. struct drm_i915_gem_object *obj;
  4579. struct sg_table *sg;
  4580. size_t bytes;
  4581. int ret;
  4582. obj = i915_gem_object_create(dev, round_up(size, PAGE_SIZE));
  4583. if (IS_ERR(obj))
  4584. return obj;
  4585. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  4586. if (ret)
  4587. goto fail;
  4588. ret = i915_gem_object_get_pages(obj);
  4589. if (ret)
  4590. goto fail;
  4591. i915_gem_object_pin_pages(obj);
  4592. sg = obj->pages;
  4593. bytes = sg_copy_from_buffer(sg->sgl, sg->nents, (void *)data, size);
  4594. obj->dirty = 1; /* Backing store is now out of date */
  4595. i915_gem_object_unpin_pages(obj);
  4596. if (WARN_ON(bytes != size)) {
  4597. DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes, size);
  4598. ret = -EFAULT;
  4599. goto fail;
  4600. }
  4601. return obj;
  4602. fail:
  4603. drm_gem_object_unreference(&obj->base);
  4604. return ERR_PTR(ret);
  4605. }